【问题标题】:React 'props' is not definedReact 'props' 未定义
【发布时间】:2020-10-16 05:25:12
【问题描述】:

有人能告诉我为什么没有定义 props 吗?在这段代码中,我需要通过这种方式调用另一个组件,

看看

我是 react 新手,谁能帮帮我?

反应代码如下:

class ViewNotas extends Component {
  constructor(props) {
    super(props);

    this.state = {
      open: [],
      newContact: false,
      edit: [],
      loading: false,
    };

    this.handleOpen = this.handleOpen.bind(this);
    this.setNewContact = this.setNewContact.bind(this);
  }

  setNewContact(set) {
    this.setState({
      newContact: set,
    });
  }

  handleOpen(id, isOpen) {
    if (!isOpen) {
      this.setState({ open: this.state.open.concat(id) });
    } else {
      this.setState({
        open: this.state.open.filter((objectId) => id !== objectId),
      });
    }
  }

  render() {
    let data = this.props.data;

    return (
      <Modal open={this.props.open} visible={this.props.visible}>
        <SC.Container>
          <SC.Header>
            <SC.Title>Notas</SC.Title>
            <SC.ButtonPlus onClick={() => props.setNewContact(true)}>
              <GoPlus size="24px" color="#FFF" />
            </SC.ButtonPlus>

【问题讨论】:

    标签: reactjs components react-props


    【解决方案1】:

    这一行

    <SC.ButtonPlus onClick={() => props.setNewContact(true)}>
    

    应该是

    <SC.ButtonPlus onClick={() => this.setNewContact(true)}>
    

    【讨论】:

      【解决方案2】:

      问题在于你正在访问 props 上的方法

                                    /* change this  */
       <SC.ButtonPlus onClick={() => this.setNewContact(true)}>
          <GoPlus size="24px" color="#FFF" />
        </SC.ButtonPlus>
      

      希望这会有所帮助!

      【讨论】:

      • setNewContact 不是作为道具传递的,而是类上的一个方法,所以它只需要是this. setNewContact(true)
      猜你喜欢
      • 2017-03-30
      • 2017-12-04
      • 1970-01-01
      • 2018-01-24
      • 1970-01-01
      • 1970-01-01
      • 2021-01-15
      • 2016-11-23
      • 1970-01-01
      相关资源
      最近更新 更多