javascript - Concat ReactElements to render -
i have if component in react straightforward:
'use strict'; var react = require('react'); module.exports = react.createclass({ render: function () { if (this.props.condition) { return this.props.children; } return false; } }); i can call like:
<if condition={somelogic}> <div>hi there</div> </if> the problem if have multiple tags inside if component:
<if condition={somelogic}> <div>container 1</div> <div>container 2</div> </if> this gives me error:
uncaught error: invariant violation: exports.render(): valid reactcomponent must returned. may have returned undefined, array or other invalid object.
here this.props.condition array of reactelement.
question: how concat array of reactelement , return one?
note: realize can put both of these divs inside 1 wrapper, sake of example (and actual problem), let's cannot that, , you'd have return multiple tags
react not support returning multiple components render. render method must return 1 element - can see issues https://github.com/facebook/react/issues/2127 , https://github.com/facebook/react/issues/2191
solution wrap props.children element, example
var if = react.createclass({ render: function () { if (this.props.condition) { return <div>{this.props.children}</div>; } return false; } });
Comments
Post a Comment