自定义道具不会传递给无状态子组件

时间:2019-07-28 20:52:44

标签: reactjs react-props

因此,在完成将this freeCodeCamp Challenge传递给无状态组件的操作后,我尝试将代码复制到codePen中以供以后参考,但这些传递没有通过。

因此,如果我分配了默认值,则默认值可以正常工作,但是如果我尝试删除默认值,则prop仅返回null,并且覆盖默认值不起作用。 (顺便说一句,当我尝试使用propTypes并将其设为必需时,它根本不会呈现)。

const CurrentDate = (props) => {
  console.log(props.date)
  return (
    <div>
      { /* change code below this line */ }
      <p>The current date is: {props.date}</p>
      { /* change code above this line */ }
    </div>
  );
};

CurrentDate.defaultProps = {date: "test"};
//CurrentDate.propTypes = {date: PropTypes.string.isRequired};

class Calendar extends React.Component {
  constructor(props) {
    super(props);
  }
  render() {
    return (
      <div>
        <h3>What date is it?</h3>
        { /* change code below this line */ }
        <CurrentDate date={Date()}/>
        { /* change code above this line */ }
      </div>
    );
  }
};

console.log(Date());

ReactDOM.render(<CurrentDate/>,$("#target")[0]); //target is the id of the only element (div) in the html

0 个答案:

没有答案