How on React.js to make a component, the contents of the dom-element of which must be fully controlled by the script?
The following code creates the Smth component, which is a div with the passed text as the content. It is guaranteed that the text property will have a string.
var Smth = React.createClass({ render: function() { return React.DOM.div(this.props, this.props.text); } }); ReactDOM.render( React.createElement(Smth, { className: 'some-class', style: { color: 'blue' }, text: "Some text goes here" // Nothing except text is here }), document.querySelector('main') ); .some-class { border: 1px solid green; background: silver; float: left; padding: .25em .5em; } <script src="https://fb.me/react-0.14.2.min.js"></script> <script src="https://fb.me/react-dom-0.14.2.min.js"></script> <main></main> How to do that:
- The reaction was correctly built and updated by the
divitself. - The written script was completely responsible for the contents of this div and could put both text and markup inside it, as well as change it independently of the component.
- A written script found out that the
textproperty has changed.
I believe that this item should be carried out by itself.
A written script is understood as a certain script inside a component that is not controlled by a reactor. There is a need to change the contents of the dom element, regardless of the reactor.
shouldComponentUpdate. Everything has its own methods of the component life cycle, and you should ask to update this or that thing or not. If it is necessary, for example, to return methods from the reactor to the top, I returned tocomponentWillMountmethods created inside the component to control it, for example, setting any values. And outside he called these methods - Vasily Barbashev