我为什么要“返回_this.state.data”而不是JSON对象

前端之家收集整理的这篇文章主要介绍了我为什么要“返回_this.state.data”而不是JSON对象 前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我有一个父组件正在执行AJAX调用获取JSON对象.我已经做了一些console.log来确保父组件中的数据是正确的,但是当我通过道具时,我得到的值为:

  1. ƒ data() {
  2. return _this.state.data;
  3. }

至此,我所做的事情似乎很简单,所以我找不到问题所在.

父组件:

  1. class InfoBox extends Component {
  2. state = {
  3. data: []
  4. };
  5. componentDidMount = () => {
  6. this.loadDonationsFromServer();
  7. setInterval(this.loadDonationsFromServer,this.props.pollInterval);
  8. };
  9. loadDonationsFromServer = () => {
  10. $.ajax({
  11. url: "https://jsonplaceholder.typicode.com/comments",dataType: "json",cache: false,success: data => {
  12. this.setState({ data });
  13. },error: (xhr,status,err) => {
  14. console.error(status,err.toString());
  15. }
  16. });
  17. };
  18. render = () => {
  19. return (
  20. <React.Fragment>
  21. <h1>Information</h1>
  22. <InfoList
  23. data={() => this.state.data}
  24. />
  25. </React.Fragment>
  26. );
  27. };
  28. }
  29. export default DonationBox;

子组件:

  1. class InfoList extends Component {
  2. constructor(props) {
  3. super(props);
  4. this.state = {
  5. data: this.props.data
  6. };
  7. }
  8. componentDidMount() {
  9. console.log(this.state.data);
  10. //logs: ƒ data() {
  11. // return _this.state.data;
  12. // }
  13. }
  14. render() {
  15. return <div> Placeholder </div>;
  16. }
  17. }
  18. export default InfoList;

我尝试在子组件中使用bind,但仍然得到了相同的结果:

  1. constructor(props) {
  2. super(props);
  3. this.state = {
  4. data: this.props.data
  5. };
  6. this.checkData = this.checkData.bind(this);
  7. }
  8. componentDidMount() {
  9. this.checkData();
  10. }
  11. checkData = () => {
  12. console.log(this.state.data);
  13. };
最佳答案
首先,是的,您应该将发送到InfoList的数据属性更改为this.state.data而不是匿名函数.因此:< InfoList data = {this.state.data} />

但是,主要问题是在子组件中使用componentDidMount,而实际上应该使用componentWillReceiveProps代替.

componentDidMount仅被调用一次,它不会等待您的AJAX

在初始渲染之前,componentDidMount生命周期挂钩被调用了一次.

在子组件的componentDidMount上,您尝试记录this.state.data-但此状态基于构造函数中设置的内容,即在您首次安装InfoList时作为数据属性传入的内容.那是[],因为InfoBox尚未从其Ajax调用接收回数据.换一种方式:

InfoList.componentDidMount() fired before InfoBox.loadDonationsFromServer() got back its response. And InfoList.componentDidMount() does not get fired again.

每当道具更改时,都会调用componentWillReceiveProps

而是,您的子组件应该使用componentWillReceiveProps生命周期挂钩.每当组件接收到新的道具时,就会调用方法.一旦父母的状态发生变化(在负荷捐赠返回之后),它将新的道具传递给孩子.在componentWillReceiveProps中,孩子可以使用这些新道具并更新其状态.

我创建了一个code sandbox,它通过一堆日志语句向您显示什么时候发生了什么,以及生命周期各个点的道具和状态.与实际执行ajax提取不同,我只是等待2秒钟以模拟提取.在InfoList.js中,当前已注释掉componentWillReceiveProps的代码;这样,您可以了解事物的现状.删除注释并开始使用componentWillReceiveProps之后,您将看到如何修复它们.

额外资源

>这是一个helpful article,几乎描述了您面临的相同问题.
> React生命周期挂钩的绝佳快速参考是React Cheat Sheet)

猜你在找的JavaScript相关文章