Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passings array as props in reactjs

Tags:

reactjs

I am new to react.

I have been experimenting on react and I got stuck on how to pass array using props.

case-1:

var c = ['program']; var Navigation = React.createClass({     getInitialState: function () {         return {             openDropdown: -1         };     },     getDefaultProps: function () {         return {             config: ['everyone']         };     },     render: function () {         return (             <div className="navigation">                 helloworld {this.props.config[0]};             </div>             );     } });  React.render(<Navigation config={c}/>, document.body); 

This is rendering helloworld program. which is expected.

Later I have tried.

case-2:

var c = ['program']; var Navigation = React.createClass({     getInitialState: function () {         return {             openDropdown: -1         };     },     getDefaultProps: function () {         return {             config: ['everyone']         };     },     render: function () {         return (             <div className="navigation">               {this.props.config} helloworld ;             </div>             );     } });  React.render(<Navigation config="React"/>, document.body); 

This is rendering React helloworld. which is expected as there are no propType defined.

next I have tried.

case-3 :

var c = ['program']; var Navigation = React.createClass({     getInitialState: function () {         return {             openDropdown: -1         };     },     getDefaultProps: function () {         return {             config: ['everyone']         };     },     render: function () {         return (             <div className="navigation">                 helloworld {this.props.config[0]};             </div>             );     } });  React.render(<Navigation config=['!!!'] />, document.body); 

Which is not rendering anything.

Later when I have changed the React.render(<Navigation config=['!!!'] />, document.body); to React.render(<Navigation config={['!!!']} />, document.body);

it rendered helloworld !!!

I have read in some tutorial that curly braces are used to pass variables so that JSX will know that they are external variable.

But why is case-3 not working with out explicit curly braces though the array is made during the call and why is it working for the case-2 where the string is made inline.

When exactly are the curly braces used?

And it would help me if some one can point me to any good book or online tutorials on react.

like image 568
starkk92 Avatar asked Aug 07 '15 17:08

starkk92


People also ask

Can I pass an array as a prop in React?

To pass an array as a prop to a component in React, wrap the array in curly braces, e.g. <Books arr={['A', 'B', 'C']} /> . The child component can perform custom logic on the array or use the map() method to render the array's elements.

Can I pass an object as a prop React?

Use the spread syntax (...) to pass an object as props to a React component, e.g. <Person {... obj} /> . The spread syntax will unpack all of the properties of the object and pass them as props to the specified component.

How do you access an array of objects in Reactjs?

We will use the map function to access each object from the array. The map() method in JavaScript creates an array by calling a specific function on each element present in the parent array. It is a non-mutating method.


1 Answers

The curly braces only need to be used within JSX elements. Like this:

<MyComponent somProp={['something']} /> 

In the case above, the {} is the way of saying: "Evaluate the expression that I passed within and pass it as a prop". Within the {} you can pass any valid JavaScript object or expression. Note that if you pass a string, and specifically for strings, you don't need the curly braces... like <MyComponent somProp="something" />.

The above code is the equivalent of this:

var myArray = ['something']; <MyComponent somProp={myArray} /> 
like image 172
André Pena Avatar answered Sep 21 '22 13:09

André Pena