3 ms·
I once wanted to use React as a virtual DOM library, nothing more. Turned out that I had to create "components" for every div I wanted to draw. Of course, this
by amelius 10y ago
I once wanted to use React as a virtual DOM library, nothing more. Turned out that I had to create "components" for every div I wanted to draw. Of course, this was a nuisance to the point that I had to let go of React.
- iLoch 10y agoThe complexity of creating a component in react is identical to Vue.
- mambodog 10y agoYou seem to be mistaken. You don't need to create components if you just want to render some divs. You can just do const {render} = require('react-dom'); const {DOM} = require('react'); render( DOM.div({className="something"}, DOM.div({className="something-else"}, 'some text', ), ), document.getElementById('root'), ); Components become useful pretty quickly as your views grow in size, but even then, function components require very little extra code to use.
- amelius 10y agoInteresting, I didn't know, although I can recall searching through the documentation for answers. Does calling the "render" function multiple times perform the DOM-diffing and updating? And can I be sure that React will not perform updates behind the scenes at other moments than when calling "render"?
- mambodog 10y agoIn answer to your first question, yes. See the documentation for `render`: https://facebook.github.io/react/docs/react-dom.html#render https://facebook.github.io/react/docs/react-dom.html#render In answer to your second question, yes, React currently only does anything as a consequence of calling `render`, or `.setState()`/`.forceUpdate()` in the case of a component class (not relevant if you're not using component classes). In future, the (currently experimental) 'fiber' render will allow some rendering to be deferred until after the current call stack has finished (to avoid blocking the UI), but there should be away to tell React to do all rendering synchronously if you desire (which is necessary for eg. rendering on the server). There's no reason why the current behavior couldn't be supported on top of the new experimental renderer though.