class Example extends React.Component {
constructor() {
super();
this.isChecked = this.isChecked.bind(this);
}
isChecked(ex){
return this.props.defaults && $.inArray(ex, this.props.defaults);
}
render() {
return (
<div className="example">
{this.props.items.map(item => (
var checked = this.isChecked({item.type});
<span key={item.type}>
<input type="checkbox" {checked ? 'checked' : ''} name="example" value={item.type} id={item.type} />
<label htmlFor={item.type}>{item.display}</label>
</span>
))}
</div>
);
}
}
In the above example, I have passed a list of items to the component this.props.items and a list of defaults this.props.defaults. For each item, if the item is in the list of defaults, I would like its state to be checked. Leaving the overall design the same, how can I do that? (Note: I realize I can redesign the application to avoid the issue of declaring the variable inside the map. That is not the question).
The above code doesn't work (obviously), I am just trying to give everyone an idea of what I'm attempting here. The error given if you run this is something like "unexpected token" at the spot where "var" is declared.