Passing/Accessing props in stateless child component

后端 未结 8 2016
南方客
南方客 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:53

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

    const ChildComponent = ({ *what goes here?* }) => (
       

    Child Component

    )

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

     const ChildComponent = props => (
       

    {props.value ? props.value : "No value."}

    );

    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."
         )
       );
     };
    

提交回复
热议问题