I am converting a project from the old React.createComponent to new the ES6 class definition. I have the following component:
class AnswerFrame extends React.Component {
constructor(props) {
super(props);
}
render() {
var props = this.props;
var selectedNumbers = props.selectedNumbers.map(function (i) {
return (
<span onClick={props.unselectNumber.bind(null, i)}>
{i}
</span>
)
});
return (
<div id="answer-frame">
<div className="well">
{selectedNumbers}
</div>
</div>
);
}
};
The idea is to define an array, selectedNumbers which is an array of spans that each have an onclick event. These are then rendered.
This code worked fine before converting to ES6 but now is giving me the following error:
Error: Objects are not valid as a React child
(found: object with keys {dispatchConfig, _targetInst, nativeEvent, type, target, currentTarget, eventPhase, bubbles, cancelable, timeStamp, defaultPrevented, isTrusted, view, detail, screenX, screenY, clientX, clientY, ctrlKey, shiftKey, altKey, metaKey, getModifierState, button, buttons, relatedTarget, pageX, pageY, isDefaultPrevented, isPropagationStopped, _dispatchListeners, _dispatchInstances}).
If you meant to render a collection of children, use an array instead or wrap the object using createFragment(object) from the React add-ons. Check the render method of `AnswerFrame`.
Why is this not working anymore?
arrayofnumber/stringorarray of objects? can you show the result ofconsole.log(this.props.selectedNumbers)?this.props.selectedNumbers. Are you sure there are numbers?keyproperty in your span elements causes this problem?