1

hiii everybody,

i'm new in react and i have lost my day at Trying to run my code

i have a dictionnary in a JS : {key, value}.

i want create some button with the name equal the key. i think i I think I need to pass my dictionary ( or an array of key, because i dont need the value associed) to my reactDom.render .

i have create this :

class ButtonList extends React.Component {
    onClick(event) {
        clictodraw("name");
    }

    render() {
        let items = this.props.items || []
        let rows = items.map(
            item => {return (
                <button onClick={this.onClick}>{item}</button>);}
        );

        return (
            {rows}
        );
    }
}



window.shareRenderFunc5 = function (tab) {
    ReactDOM.render(
            <ButtonList items={tab}/>,
        document.getElementById('app')
    );
}

but he give me this message :

Error: ButtonList.render(): A valid React element (or null) must be returned. You may have returned undefined, an array or some other invalid object.

i think my problem is on the Data input to props. if you know what is the problem, please tell me.

thanks for your help

3
  • 1
    write it like this: return ( <div> {rows} <.div> ); wrap the rows with div because rows will be an array and we can't return more than one children. Commented Jun 16, 2017 at 17:36
  • Another possible issue is that when you map array elements to components, you need to give each one a unique key prop (e.g. their index in the array) Commented Jun 16, 2017 at 18:18
  • yes i have see the warning in my browser for the key, i will change that. thank you for your help. thanx you very much Commented Jun 16, 2017 at 20:07

1 Answer 1

1

You need to change your render() function so that it returns a single, top level element, with sub elements inside. Basically, every React component needs to have a single top level node that represents the component:

return (
  <div>
    {rows}
  </div>
)

Edit: With React 16 this answer is no longer correct. You can now return an array of JSX nodes from a component's render function.

Sign up to request clarification or add additional context in comments.

3 Comments

or just return rows ?
@AhmadBamieh nope, rows will be an array we need to wrap that by a div (single element).
Thank you very much, I did not know that. thx you ^^

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.