Passing/Accessing props in stateless child component

后端 未结 8 1978
南方客
南方客 2020-12-05 10:04

I know you can pass all a react components props to it\'s child component like this:

const ParentComponent = () => (
   

Parent

相关标签:
8条回答
  • 2020-12-05 11:06

    Are you looking for the ES6 named argument syntax (which is merely destructuring) ?

    const ChildComponent = ({ propName }) => (
        <div>
         <h1>Child Component</h1>
     </div>
    )
    
    const ChildComponent = (props) => ( // without named arguments
        <div>
         <h1>Child Component</h1>
     </div>
    )
    

    Optionally there is a second argument to your function depending of whether you specified a context for your component or not.

    Perhaps it would be more helpful wityh a links to the docs. As stated in the first article about functional components. Whatever props passed on to the component is represented as an object passed as first argument to your functional component.

    To go a little further, about the spread notation within jsx.

    When you write in a component :

    <Child prop1={value1} prop2={value2} />
    

    What your component will receive is an plain object which looks like this :

    { prop1: value1, prop2: value2 }
    

    (Note that it's not a Map, but an object with only strings as keys).

    So when you're using the spread syntax with a JS object it is effectively a shortcut to this

    const object = { key1: value1, key2: value2 }
    <Component {...object}/>
    

    Is equivalent to

    <Component key1={value1} key2={value2} />
    

    And actually compiles to

    return React.createElement(Component, object); // second arg is props
    

    And you can of course have the second syntax, but be careful of the order. The more specific syntax (prop=value) must come last : the more specific instruction comes last.

    If you do :

    <Component key={value} {...props} />
    

    It compiles to

    React.createElement(Component, _extends({ key: value }, props));
    

    If you do (what you probably should)

    <Component {...props} key={value} />
    

    It compiles to

    React.createElement(Component, _extends(props, { key: value }));
    

    Where extends is *Object.assign (or a polyfill if not present).

    To go further I would really recommend taking some time to observe the output of Babel with their online editor. This is very interesting to understand how jsx works, and more generally how you can implement es6 syntax with ES5 tools.

    0 讨论(0)
  • 2020-12-05 11:08

    I thought I would add a simple ES2015, destructuring syntax I use to pass all props from a functional parent to a functional child component.

    const ParentComponent = (props) => (
      <div>
        <ChildComponent {...props}/>
      </div>
    );
    

    Or if I have multiple objects (props of parent, plus anything else), I want passed to the child as props:

    const ParentComponent = ({...props, ...objectToBeAddedToChildAsProps}) => (
      <div>
        <ChildComponent {...props}/>
      </div>
    );
    

    This destructuring syntax is similar to the above answers, but it is how I pass props along from functional components, and I think it is really clean. I hope it helps!

    0 讨论(0)
提交回复
热议问题