React - TypeError: this.props.AccountId is not a function

React - TypeError: this.props.AccountId is not a function

我已经对这个问题进行了研究,但我似乎找不到解决问题的办法。

同一问题的其他 post 似乎与绑定问题有关,但我认为这不是这里的问题。

当我加载页面时,prop 应该会显示,但它什么也没显示。

当我以文本形式书写时,出现错误:TypeError: this.props.AccountId is not a function", 这就是我被困的地方。

import React from 'react';

 class Accountparent extends React.Component {
  constructor(props) {
    super(props);
    this.onFocusChange = this.onFocusChange.bind(this);
    this.state = {accountobj: ''};
  }

   onFocusChange(value){
    this.setState({accountobj: value});
  }
  render() {
    return (
      <div>
        <Account value={this.state.accountobj} AccountId={this.onFocusChange}/>
      </div>
    )
  }
}

class Account extends React.Component {
  constructor(props) {
    super(props);
    this.EditAccount = this.EditAccount.bind(this)
    this.state = {
      accounts: [],
    };
  }
  EditAccount(e){
    this.props.AccountId(e.target.value)
  }


  render() {
    const value = this.props.AccountId;
    return (
      <div>
        <div>The props is: {this.props.dataid}</div>
        <div>Write to textfield to pass data to parent: <input value={value} type="text" onChange={this.EditAccount}/></div>
      </div>

    )
  }
}
export default Account;

编辑: 正如@Taki 所建议的,我应该导出父项。 这就是解决方案

检查你的代码,

EditAccount(e){
    this.props.AccountId(e.target.value)
  }


  render() {
    const value = this.props.AccountId;
    . . .

在 EditAccount 方法中,您像使用函数一样使用它。 在 render 方法中,您从中派生 "value" 。 它可以是一个值或一个函数。不是两者。

正如@Taki 所建议的,我应该导出父项。这就是解决方案