index.jsx
import React from 'react'
import { render } from 'react-dom'
import Main from './Main'
window.DatoCmsPlugin.init((plugin) => {
plugin.startAutoResizer()
const container = document.createElement('div')
document.body.appendChild(container)
render(<Main plugin={plugin} />, container)
})
Main.jsx
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import connectToDatoCms from './connectToDatoCms';
import './style.sass';
@connectToDatoCms(plugin => ({
developmentMode: plugin.parameters.global.developmentMode,
fieldValue: plugin.getFieldValue(plugin.fieldPath),
}))
export default class Main extends Component {
static propTypes = {
fieldValue: PropTypes.bool.isRequired,
}
render() {
const { fieldValue } = this.props;
return (
<div className="container">
{JSON.stringify(fieldValue)}
</div>
);
}
}
connectToDatoCms.jsx
import React, { Component } from 'react'
export default mapPluginToProps => BaseComponent => (
class ConnectToDatoCms extends Component {
constructor (props) {
super(props)
this.state = mapPluginToProps(props.plugin)
}
componentDidMount () {
const { plugin } = this.props
this.unsubscribe = plugin.addFieldChangeListener(plugin.fieldPath, () => {
this.setState(mapPluginToProps(plugin))
})
}
componentWillUnmount () {
this.unsubscribe()
}
render () {
return <BaseComponent {...this.props} {...this.state} />
}
}
)
Я использовал эту команду для генерации стартового кода для datoCMS плагин, npx -p yo -p generator-datocms-plugin -c 'yo datocms-plugin'
.
Что такое @
в @connectToDatoCms
, Main.jsx.