Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invariant Violation: Objects are not valid as a React child ... If you meant to render a collection of children, use an array instead or wrap the object using createFragment(object) #5168

Closed
almeynman opened this issue Oct 14, 2015 · 2 comments

Comments

@almeynman
Copy link

In my component's render function I have:

render() {
    const items = ['EN', 'IT', 'FR', 'GR', 'RU'].map((item) => {
      return (<li onClick={this.onItemClick.bind(this, item)} key={item}>{item}</li>);
    });
    return (
      <div>
        ...
                <ul>
                  {items}
                </ul>
         ...
      </div>
    );
  }

everything renders fine, however when clicking the

  • element I receive the following error:

    Uncaught Error: Invariant Violation: Objects are not valid as a React child (found: object with keys {dispatchConfig, dispatchMarker, nativeEvent, target, currentTarget, type, 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, _dispatchIDs}). 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 `Welcome`.
    

    If I change to this.onItemClick.bind(this, item) to (e) => onItemClick(e, item) inside the map function everything works as expected.

  • @seekshiva
    Copy link

    When you do this.onItemClick.bind(this, item), item will be passed as the first argument, and event will go in as the second argument.

    But you said that e => onItemClick(e, item) worked. So maybe onItemClick is expecting arguments in the wrong order? [e, item] instead of [item, e].

    Can you add logging statements in onItemClick to figure out where exactly the error is being thrown?

    @almeynman
    Copy link
    Author

    You are absolutely right! Sorry for wasting your time :( Seems like I need to have some sleep...

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    None yet
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants