I have the following component (Section) that render a list of Field components:
class Section extends React.Component {
renderFields() {
const subfields = this.props.data_fields || [];
const fieldsList = subfields.map((field_data) =>
this.addField(field_data)
);
return (
fieldsList
)
}
addField(field_data) {
return (
<Field name='foo' type='bar' />
)
}
render() {
return <div>
{this.renderFields()}
</div>;
}
}
I would like to replace the addField method, using a dynamically created Component like this:
addField(field_data) {
....
let field = new Field({name: name, type: type}) // Component, not ReactElement
....
// Some logic using 'field' here....
return (
{field}
)
}
But I get this error:
Error: Objects are not valid as a React child (found: object with keys {field}).
So I tried to modified the return into:
return (
<div>
{field.render()}
</div>
)
This output correctly on the page, but when I explore the DOM with React Developer Tool, the element became a (previously it was a )
I think I do it wrong because when later-on I change the state of one of these dynamically added Field, I get the following error:
Warning: setState(...): Can only update a mounted or mounting component. This usually means you called setState() on an unmounted component. This is a no-op. Please check the code for the Field component.
How can I use a dynamically created component into a JSX snippet ?