I'm looking for advice on the best way to dynamically create elements from JSON in React using mapping with components.
I figure we can have each input type as a separate component class and construct the element passing in props and bind the changes.
E.g. the JSON:
{
"type": "text",
"name": "FirstName",
"class": "text",
"placeholder": "Enter first name"
},
{
"type": "text",
"name": "Surname",
"class": "text",
"placeholder": "Enter surname"
},
React:
class InputText extends React.Component {
constructor(props) {
super(props);
this.changeValue = this.changeValue.bind(this);
}
render() {
return (
<div className={className}>
<label htmlFor={this.props.name}>{this.props.title}</label>
<input
onChange={this.changeValue}
name={this.props.name}
type={this.props.type}
value={this.props.value}
/>
</div>
);
}
}
Any advice on the best way to iterate through (and validate) each item in the JSON? Am I on the right track here? Thanks!