简体   繁体   English

将更新状态作为道具传递

[英]Passing updated state as props

I'm relatively new to React and wondering if you could take a look at this problem if possible. 我对React比较陌生,想知道如果可能的话你是否可以看看这个问题。 I'm making an ajax call to an API on a click event and storing the results in the 'SearchButtons' component's state. 我正在对Click事件上的API进行ajax调用,并将结果存储在'SearchButtons'组件的状态中。

The state is then passed into the child component via the props. 然后通过props将状态传递给子组件。 However the props in the child component ( SearchForm ) equals the previous state on the parent component and is not updated when the user clicks one of the buttons. 但是,子组件( SearchForm )中的props等于父组件上的先前状态,并且在用户单击其中一个按钮时不会更新。

I'm wondering if there is a way around this, maybe updating the component before the props are sent to the child component? 我想知道是否有办法解决这个问题,也许在将道具发送到子组件之前更新组件?

import React from 'react';
import SearchForm from './SearchForm'

class SearchButtons extends React.Component {
  constructor(){
    super();
    this.state = {
      data: {}
    }
  }

  update(event){
    $.ajax({
      url: 'http://example/api/' + event.target.value,
      dataType: 'json',
      cache: false,
      success: function(data) {
        this.setState({ data: data })
      }.bind(this)
    });
  }

  render() {
    return (
      <div>
        <button type="button" value="people" onClick={this.update.bind(this)} className="btn btn-primary">People</button>
        <button type="button" value="car" onClick={this.update.bind(this)} className="btn btn-primary">Car</button>
        <button type="button" value="planet" onClick={this.update.bind(this)} className="btn btn-primary">Planet</button>
        <SearchForm data={this.state.data} />
      </div>
    )
  }
}

export default SearchButtons

Please see example of the SearchForm component below 请参阅下面的SearchForm组件示例

import React from 'react';

class SearchForm extends React.Component {

  componentWillReceiveProps(){
    console.log(this.props.data);
  }

  render(){
    return (
      <form className="form-inline">
        <div className="form-group">
          <label className="sr-only" for="exampleInputEmail3"></label>
          <input type="text" className="form-control" placeholder="Enter text..." />
        </div>
        <button type="submit" className="btn btn-default">Search</button>
      </form>
    )
  }
}

export default SearchForm

componentWillReceiveProps receives the next props as the first argument, so by logging this.state.props you are seeing the current props, not the ones that are being received. componentWillReceiveProps接收下一个道具作为第一个参数,因此通过记录this.state.props您将看到当前的道具,而不是正在接收的道具。

Try to log the next ones like this: 尝试记录下面这样的:

componentWillReceiveProps(nextProps){
    console.log(nextProps.data);
}

Of course, you can also check what the new props are after they have been updated by calling the componentDidUpdate lifecycle method instead of (or alongside, if you want to monitor both steps) componentWillReceiveProps : 当然,您也可以通过调用componentDidUpdate生命周期方法来检查新道具的更新componentDidUpdate而不是(或者如果您想监视这两个步骤的话,还是旁边的那些) componentWillReceiveProps

componentDidUpdate(prevProps, prevState) {
    console.log(this.props.data);
}

Take a look a the lifecycle methods if you want to know more! 如果您想了解更多信息,请查看生命周期方法

It appears as though you're doing things correctly, and if you render some data from the props in the JSX you'll see the data update as you expect. 看起来好像你正在做的事情,如果你从JSX中的道具渲染一些数据,你会看到你想象的数据更新。

componentWillRecieveProps as a lifecycle method is very semantic in what it means, the component WILL receive props but hasn't yet. componentWillRecieveProps作为一种生命周期方法,其意义非常具有语义性,组件将获得道具但尚未获得道具。 If you log this.props you'll see the old props. 如果您记录this.props,您将看到旧的道具。 Log the next Props to see the update 记录下一个Props以查看更新

componentWillReceiveProps(nextProps) {
  console.log('new props: ', nextProps);
}

This is a very good article that very concisely breaks down each stage of the component life cycle and shows you how each method works: 这是一篇非常好的文章,它非常简洁地分解了组件生命周期的每个阶段,并向您展示了每种方法的工作原理:

http://busypeoples.github.io/post/react-component-lifecycle/ http://busypeoples.github.io/post/react-component-lifecycle/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM