无法设置状态数据

时间:2018-12-07 13:44:15

标签: javascript reactjs

我是新来的程序员。这可能是愚蠢的错误,但是,我无法访问智能组件中的状态数据。

以下是我的代码。

Sub Example()

    Dim wrkSht As Worksheet
    Dim rFound As Range
    Dim rng1 As Range
    Dim cht As ChartObject
    Dim DisplayRange As Range

    'The code needs to know which sheet you're talking about.
    'Not designating the sheet means it will look at the currently active sheet.
    Set wrkSht = ThisWorkbook.Worksheets("Sheet1")

    'The chart will be built to cover this range.
    Set DisplayRange = wrkSht.Range("D5:H20")

    'Try and find the cell and store it's reference in a range variable.
    'Note that it's using wrkSht variable to identify the sheet that cells are on.
    Set rFound = wrkSht.Cells.Find( _
        What:="SUM(FRD End Analysis)", _
        After:=wrkSht.Range("A1"), _
        LookIn:=xlValues, _
        LookAt:=xlWhole, _
        SearchOrder:=xlByRows, _
        SearchDirection:=xlNext)

    'If it hasn't been found then rFound will be nothing, otherwise it will not be nothing.
    If Not rFound Is Nothing Then

        'Here the cell 1 column to the right of rFound is considered to be cell "A1".
        'So if value is found in cell B2 then .Range("A1:E2") will reference cells C2:G3 (C2 is considered to be A1)
        Set rng1 = Union(wrkSht.Range("C1:G1"), rFound.Offset(, 1).Range("A1:E2"))

        'Create a chart container.
        Set cht = wrkSht.ChartObjects.Add _
            (DisplayRange.Left, DisplayRange.Top, DisplayRange.Width, DisplayRange.Height)

        'Build the chart within the chart container.
        With cht
            .Chart.ChartType = xlColumnClustered

            'Create the series from rng1.
            With .Chart.SeriesCollection.NewSeries
                .Name = "First Row"
                .XValues = rng1.Rows(1)
                .Values = rng1.Rows(2)
            End With

            With .Chart.SeriesCollection.NewSeries
                .Name = "Second Row"
                .XValues = rng1.Rows(1)
                .Values = rng1.Rows(3)
            End With

        End With

    End If

End Sub

如果我在随后发出警报数据,则该数据在那里。

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.

任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:2)

立即尝试: 您需要将此关键字与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 

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

尝试一下可能有帮助

     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;