【问题标题】:I passed a prop "function" to a child component => Error props is undefined我将道具“函数”传递给子组件=>错误道具未定义
【发布时间】:2019-07-11 06:22:43
【问题描述】:

我是 ReactJs 的新手,我正在尝试将一个函数传递给我的子组件,以更新父组件中的状态!

子组件好像找不到传递的函数。

单击时收到的错误消息: TypeError: this.props.cencelLogin 不是函数

另一个当我访问子组件时“这是一个不会使程序崩溃的控制台错误”: 警告:在现有状态转换期间无法更新(例如在render 内)。渲染方法应该是 props 和 state 的纯函数。

我 console.log(this.props) = 我找不到任何 getBackCancelLoginForm 存在的证据。 “我是新手”

我看了一些组件通信的视频。似乎这种技术正在奏效;但是,由于一个小错误,这种技术对我不起作用。

这是我的父类组件:

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

    this.state = {
      show: 'Defualt'
    };

    this.getBackCancelLoginForm = this.getBackCancelLoginForm.bind(this);
  }

  getBackCancelLoginForm = (showVal) => {
    this.setState({ show: showVal });
  }

  render() {

    this.CheckShowMenu();

    return (
      <div>

        { this.state.show === "Defualt" ? <DefualtMenu  cencelLogin={this.getBackCancelLoginForm}/> : (<div> </div>)}

      </div>

    );
  }
}

这是我的子类组件:

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

      this.state = {
        cancelSignIn: "Defualt",
      };

      this.cancel = this.cancel.bind(this);
    }

    cancel(props){
      this.props.cencelLogin(this.state.cancelSignIn);
    }

    render() {
      return (
        <div>

        <div className="headerCancelBox" onClick={this.cancel.bind(this)}>

        </div>
      );
    }
}

在实际结果中:形式上的歌唱应该是绝望的。

当点击子组件上的“X”时 -> 函数取消应该触发它调用 prop 函数。 这会导致子函数被另一个子函数“默认菜单”替换

【问题讨论】:

    标签: reactjs


    【解决方案1】:

    我发现我的愚蠢错误!我不得不承认我花了几个小时来解决这个问题。

    我会把答案留在这里永远得到同样的问题!

    我只是将道具传递给了错误的子函数。 :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-07
      • 2019-07-03
      • 2020-07-03
      • 2021-11-10
      • 1970-01-01
      • 2021-11-18
      • 2023-04-08
      • 2020-09-01
      相关资源
      最近更新 更多