【问题标题】:Issue with react state not updating/incrementing反应状态不更新/增加的问题
【发布时间】:2017-12-30 12:09:33
【问题描述】:

我正在尝试通过单击一些调用方法来增加状态值的文本来进行分页。然后将状态值传递给 axios 调用,然后该调用应该调用下一页。然而,我注意到,虽然从渲染函数的 console.log 中状态开始增加,但 axios 调用并没有被新的状态值再次调用。有人知道我该如何解决这个问题吗?

constructor(props) {
    super(props);
    this.state = {
        people: [],
        planets: [],
        page: 1
    };
    this.pageIncrementer = this.pageIncrementer.bind(this);
}

componentWillMount() {
    let page = this.state.page;
    axios({
        method: 'GET',
        url: `http://localhost:3008/people?_page=${page}&_limit=10`
    }).then((response) => {
        this.setState({
            people: response
        });
    }).catch((error) => {
        console.log('There is an error in the Card axios call for people: ', error);
    })
    axios({
        method: 'GET',
        url: `http://localhost:3008/planets?_page=${page}&_limit=10`
    }).then((response) => {
        this.setState({
            planets: response
        });
    }).catch((error) => {
        console.log('There is an error in the Card axios call for planets: ', error);
    })
}

pageIncrementer() {
    this.setState({
        page: this.state.page + 1
    });
}

【问题讨论】:

    标签: javascript reactjs axios


    【解决方案1】:

    componentWillMount 只调用一次,你需要componentDidUpdate https://facebook.github.io/react/docs/react-component.html#componentdidupdate

    let getData = () => Math.random();
    
    class Example extends React.Component{
            constructor(props) {
              super(props);
              this.handleChange = this.handleChange.bind(this)
              this.state = {
                    name: ''
              };
            }
    
            componentWillMount(){
                 console.log('componentWillMount')
            }
    
            componentDidUpdate(){
                 console.log('componentDidUpdate') 
            }
    
            handleChange(e) {
              this.setState({
                name: this.props.getData()
              });
            }
    
    
            render() {
                return <div className="widget"> 
                {this.state.name}
                <button onClick={this.handleChange}>Inc</button>
    
                </div>;
            }
          }
    
          React.render(<Example getData={getData}/>, document.getElementById('container'));
    

    编辑(替代方式):

    let getData = () => Math.random();
    
    class Example extends React.Component{
            constructor(props) {
              super(props);
              this.makeRequest = this.makeRequest.bind(this)
              this.state = {
                    page:1,
                    name:''
              };
            }
    
            makeRequest(next){
                 fetch('https://jsonplaceholder.typicode.com/posts/'+this.state.page)
                 .then(
                    result => {
                     console.log('do')
                     return result.json()}
                 )
                 .then(
                     (resp) => this.setState({
                     name:resp, page:this.state.page+1})
                 )
            }
    
    
            render() {
                return <div className="widget"> 
                {this.state.name}
                <button onClick={this.makeRequest}>Request</button>
    
                </div>;
            }
          }
    
          React.render(<Example getData={getData}/>, document.getElementById('container'));
    

    【讨论】:

    • 你能从我的代码中给我一个关于如何使用 componentDid Update 的例子吗?我不太熟悉它,将不胜感激。
    • 我正在让分页工作,但它似乎处于无限循环中,我什至没有点击下一页按钮。 NVM 我明白了,感谢您的帮助,我必须在 componentdidmount 中添加一个 if 条件来检查状态是否发生了变化。
    猜你喜欢
    • 2018-03-28
    • 1970-01-01
    • 2021-01-28
    • 2023-04-02
    • 2021-01-17
    • 2020-12-28
    • 2021-01-14
    相关资源
    最近更新 更多