Passing/Accessing props in stateless child component

后端 未结 8 1977
南方客
南方客 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 10:47
    const ParentComponent = (props) => (
       <div>
         <h1>Parent Component</h1>
         <ChildComponent {...props} />
       </div>
    );
    
    const ChildComponent = ({prop1, ...rest}) =>{
      <div>
        <h1>Child Component with prop1={prop1}</h1>
        <GrandChildComponent {...rest} />
      </div>
    }
    
    const GrandChildComponent = ({prop2, prop3})=> {
      <div>
        <h1>Grand Child Component with prop2={prop1} and prop3={prop3}</h1>
      </div>
    }
    
    0 讨论(0)
  • 2020-12-05 10:53

    But how do you then retrieve those props if the child component is stateless?

    const ChildComponent = ({ *what goes here?* }) => (
       <div>
          <h1>Child Component</h1>
       </div>
    )
    

    ChildComponent holds the name and the props will be the argument in the arrow function syntax just as you need:

     const ChildComponent = props => (
       <div>
         <p>{props.value ? props.value : "No value."}</p>
       </div>
     );
    

    If you Babel-it it will create something like this:

     var ChildComponent = function ChildComponent(props) {
       return React.createElement(
         "div",
         null,
         React.createElement(
           "p",
           null,
           props.value ? props.value : "No value."
         )
       );
     };
    
    0 讨论(0)
  • 2020-12-05 10:54

    You can use Spread Attributes reducing code bloat. This comes in the form of {'somearg':123, ...props} or {...this.props}, with the former allowing you to set some fields, while the latter is a complete copy. Here's an example with ParentClass.js :

    import React from 'react';
    import SomeComponent from '../components/SomeComponent.js';
    
    export default class ParentClass extends React.Component {
        render() {
            <SomeComponent
                {...this.props}
            />
        }
    }
    

    If I do, <ParentClass getCallBackFunc={() => this.getCallBackFunc()} />, or if I do <ParentClass date={todaysdatevar} />, the props getCallBackFunc or date will be available to the SomeComponent class. This saves me an incredible amount of typing and/or copying/pasting.

    Source: ReactJS.org: JSX In Depth, Specifying the React Element Type, Spread Attributes. Official POD:

    If you already have props as an object, and you want to pass it in JSX, you can use ... as a “spread” operator to pass the whole props object. These two components are equivalent:

     return <Greeting firstName="Ben" lastName="Hector" />;
    }
    
    function App2() {
     const props = {firstName: 'Ben', lastName: 'Hector'};
     return <Greeting {...props} />;
    }```
    

    Now, let's apply this to your code sample!

    const ParentComponent = (props) => (
       <div>
         <h1>Parent Component</h1>
         <ChildComponent {...props} />
       </div>
    );
    
    0 讨论(0)
  • 2020-12-05 10:57

    When you write

    const ChildComponent = ({ someProp }) => (
       <div>
          <h1>Child Component {someProp}</h1>
       </div>
    )
    

    From all the props that you are passing to the childComponent you are just destructuring to get only someProp. If the number of props that you want to use in ChildComponents are countable(few) amongst the total number of props that are available, destructuring is a good option as it provides better readability.

    Suppose you want to access all the props in the child component then you need not use {} around the argument and then you can use it like props.someProp

    const ChildComponent = (props) => (
       <div>
          <h1>Child Component {props.someProp}</h1>
       </div>
    )
    
    0 讨论(0)
  • 2020-12-05 10:59

    For some reason, what seems to work for me is a variation on Shubham's answer above:

    const ChildComponent = props => (
       <div>
          <h1>Child Component {props[0].someProp}</h1>
       </div>
    )
    
    0 讨论(0)
  • 2020-12-05 11:04

    Using this

    const ParentComponent = ({ prop1, prop2, prop3 }) => (
       <div>
         <h1>Parent Component</h1>
         <ChildComponent {...{ prop1, prop2, prop3 }} />
       </div>
    );
    
    const ChildComponent = ({ prop1, prop2, prop3 }) =>{
      <div>
        <h1>Child Component with prop1={prop1}</h1>
        <h1>Child Component with prop2={prop2}</h1>
        <h1>Child Component with prop2={prop3}</h1>
      </div>
    }
    
    0 讨论(0)
提交回复
热议问题