Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React.js -- How to pass properties object to child component?

I have a component called tileGroup that has a property that is a collection(array) of other properties.

The parent component(tileGroup) renders a list of child components by using each set of properties in the collection to create a new component.

Right now I am individually mapping each property from the set to the child component but this will become cumbersome if the property count for a component increases and I'm sure there is a cleaner, simpler way to do it...

How can I pass the full set of properties on to the child component without remapping each property?

Example code:

tileGroupData = {someProperty: 'something', someOtherProperty:'something',                  tiles: [{vsize:1, hsize:2, etc...}, {vsize:2,hsize:3,title:'whatever'}]}; 

and then component creation..

var tileGroup = React.createClass({     render: function() {        var thechildren = this.props.tiles.map(function(tile)        {            //this is what I DON'T want to be doing.             return <tileSimple vsize = {tile.vsize} hsize = {tile.hsize} content = {tile.content}/>;             //what I DO want to be doing            return <tileSimple allTheProps = {tile}/>;         }); 
like image 612
Matt Foxx Duncan Avatar asked Jan 03 '14 21:01

Matt Foxx Duncan


People also ask

How do you pass property from parent to child in React?

To pass data from a child component to its parent, we can call a parent function from the child component with arguments. The parent function can be passed down to the child as a prop, and the function arguments are the data that the parent will receive.

How do I pass Props to child component?

There is no way to pass props up to a parent component from a child component. We will revisit this caveat later in this tutorial. It's also important to note that React's props are read only (immutable). As a developer, you should never mutate props but only read them in your components.

How do you pass an object to a component in 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. Copied!

How do you pass value to child component in React?

Firstly, let's pass data between a parent component and a child component. . First, you'll need to create two components, one parent and one child. Next, you'll import the child component in the parent component and return it. Then you'll create a function and a button to trigger that function.


2 Answers

Apparently transferPropsTo is being deprecated. With recent versions of React you can use JSX spread attributes:

return <tileSimple {...tile} />; 

More info about this here: Deprecating transferPropsTo

like image 120
actionshrimp Avatar answered Sep 20 '22 21:09

actionshrimp


For those use cases, the easiest thing is to fallback to the JS API instead of JSX.

return tileSimple(tile); 

To understand why it works, look at the generated version of the version you want using the JSX Compiler tool ( http://facebook.github.io/react/jsx-compiler.html )

<tileSimple vsize = {tile.vsize} hsize = {tile.hsize} content = {tile.content}/>; tileSimple( {vsize:  tile.vsize, hsize:  tile.hsize, content:  tile.content}); 
like image 36
Vjeux Avatar answered Sep 18 '22 21:09

Vjeux