代码之家  ›  专栏  ›  技术社区  ›  ketan

无法设置状态数据

  •  1
  • ketan  · 技术社区  · 7 年前

    下面是我的代码。

    import React, { Component } from 'react';
    
    class App extends Component {
        constructor() {
            super();
    
            this.state = { 
                resData: [], 
            }
        }
    
        componentDidMount() {
            fetch(`http://someurl.com/something`)
            .then(response => response.json())
            .then(result => { alert(result.data[0].title); this.setState({ resData: result.data }));
    
        }
    
        render() {
            return (
                <div>
                    <Header />
                    <ErrorBoundary>
                        <Content data={ this.state.resData } />
                    </ErrorBoundary>
                    <Footer />
                </div>
            );
        }
    
    export default App;
    

    如果我在下面警告数据,那么它就在那里。

    .then(result => { alert(result.data[0].title) setState({ resData: result.data })); //Here i can see my data.
    

    我想将此状态数据传递给我的组件。但是,没有数据。

    <Content data={ this.state.resData } />
    

    任何帮助都将不胜感激。

    3 回复  |  直到 7 年前
        1
  •  2
  •   Shiva Kumar N    7 年前

    请立即尝试: 您需要将此关键字与setState()一起使用

    import React, { Component } from 'react';
    
    class App extends Component {
    constructor() {
    super();
      this.state = {
        resData: [],
      } 
    }
    
    componentDidMount() {
    fetch(`http://someurl.com/something`)
      .then(function (response) {
        return response.json()
      })
      .then(function (result) {
        this.setState({ resData: result.data })
      })
      .catch(function (error) {
        alert("Username password do not match")
      })
    }
    
    
    
    render() {
    const { resData } = this.state;
    return (
      <div>
        {resData &&
          <Header />
          <ErrorBoundary>
            <Content data={resData} />
          </ErrorBoundary>
          <Footer />
            }
      </div>
     );
    }
    
    export default App;
    
    Check it now 
    
        2
  •  0
  •   Dmitriy    7 年前

    警报在setState完成之前运行,请尝试将警报作为对setState的回调运行:

    componentDidMount() {
            fetch(`http://someurl.com/something`)
            .then(response => response.json())
            .then(result => this.setState({ resData: result.data }), () => {
              alert(this.state.resData);
            });
        }
    
        3
  •  0
  •   devedv    7 年前

    试试这个也许会有帮助

         import React, { Component } from 'react';
    
            class App extends Component {
                constructor() {
                    super();
    
                    this.state = { 
                        resData: [], 
                    }
                }
    
                componentDidMount() {
        var that = this;
                    fetch(`http://someurl.com/something`)
                    .then(response => response.json())
                    .then(result => { alert(result.data[0].title); that.setState({ resData: result.data }));
    
        alert(that.state.resData);
                }
    
                render() {
    var that = this;
                    return (
                        <div>
                            <Header />
                            <ErrorBoundary>
                                <Content data={ that.state.resData } />
                            </ErrorBoundary>
                            <Footer />
                        </div>
                    );
                }
    
            export default App;