已被删除4

问题描述 投票:0回答:3

此已被删除.........

d
3个回答
2
投票

您忘了在箭头功能内调用该功能:

        onDateChange={(date) => this.props.onDateChange(date)}

1
投票

要将方法传递给孩子,请删除.bind(this):

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

this.state = {
  //some other props
  currentDate: moment()
};

componentDidMount() {
  this.fetchData();
}

fetchData() {
  this.setState({
    //set some state here
  });
  //Some Api call
}

onDateChange(currentDate) {
  this.setState({
    currentDate
  });
}
//doing some stuff to render a table
return (
  <div>
    <ChildComponent
      currentDate={this.state.currentDate}
      // Removed .bind(this)
      // added missing parameter
      onDateChange={(date) => this.onDateChange(date)}
    />
    {currentView}
  </div>
);
}

render() {
 return (
  <div>
    //renders table
  </div>
);

0
投票

您是否可以共享完整的文件,包括导入类和导出?

从共享的这部分代码中,我可以看到一些问题:

  • 这两个类的构造函数都缺少右括号;
  • Parent中的return(//做一些事情来渲染表格)被放错了位置。您是否要将其放在render方法中?

也许是这样?

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

    this.state = {
      //some other props
      currentDate: moment()
    };
  }

  componentDidMount() {
    this.fetchData();
  }

  fetchData() {
    this.setState({
      //set some state here
    });
    //Some Api call
  }

  onDateChange(currentDate) {
    this.setState({
      currentDate
    });
  }

  render() {
    //doing some stuff to render a table
    return (
      <>
        <div>
          <ChildComponent
            currentDate={this.state.currentDate}
            onDateChange={this.onDateChange.bind(this)}
          />
          {currentView}
        </div>
        <div>
          {/* renders table */}
        </div>
      </>
    );
  }
}

currentView这里仍然缺少一些片段,因此,如果您能提供更多背景信息,那将是很好的。

希望对您有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.