How to set component default props on React component

后端 未结 9 894
孤街浪徒
孤街浪徒 2020-12-07 13:52

I use the code below to set default props on a React component but it doesn\'t work. In the render() method, I can see the output \"undefined props\" was printe

9条回答
  •  爱一瞬间的悲伤
    2020-12-07 14:09

    You can set the default props using the class name as shown below.

    class Greeting extends React.Component {
      render() {
        return (
          

    Hello, {this.props.name}

    ); } } // Specifies the default values for props: Greeting.defaultProps = { name: 'Stranger' };

    You can use the React's recommended way from this link for more info

提交回复
热议问题