【问题标题】:Do i really need to use forceUpdate with fetch API and ReactJS我真的需要将 forceUpdate 与 fetch API 和 ReactJS 一起使用吗
【发布时间】:2017-02-21 10:22:02
【问题描述】:

我正在使用 React 和 fetch() 开发 UI,我最终这样做了:

getOperatorsList: function ( obj ) {
    fetch( 'http://x.x.x.x/operators.list',
        {
            method: 'GET',
            credentials: 'include'
        }
    ).then( function ( response ) {
            return response.json()
        } ).then( function ( json ) {
        if ( json.statusCode === 3 ) {
            cookieService.unsetCookie( 'sessId' );
        }
        obj.setState( { data: json }, () => obj.forceUpdate() );
    } ).catch( function ( ex ) {
        console.log( 'parsing failed', ex );
    } )

}

这在我的组件 Operators 中调用,看起来像这样

var Operators = React.createClass( {

    getInitialState: function () {
        return {
            data: [{ "data": "Loading" }]
        }
    },

    componentDidMount: function () {
        operatorsService.getOperatorsList( this );
    },

    render: function () {
        return (
            <div>
                <Row >
                    <Col>
                        <DataTablesCustom data={this.state.data} />
                    </Col>
                </Row>
            </div>
         );
     }
});

我已经看过this question,但代码对我不起作用。

这很好用,但我真的需要使用forceUpdate() 还是有办法让代码“更干净”?

编辑:在子组件中有一个setState,看起来像这样this.setState({stuff: stuff}, this.function()});。将setState 更改为this.setState({stuff: stuff}, () =&gt; this.function()}); 后,我能够删除forceUpdate()

【问题讨论】:

  • 没有 ?
  • 没有它,我想要的数据在页面加载时不会显示。
  • 所以你有你的答案,你必须使用它;)(这段代码对我来说很清楚)
  • 这主要是关于 ReactJS 文档如何说应该避免 forceUpdate() 但我想要另一个观点。谢谢:)
  • @Steeve Pitis 代码看起来一点也不干净。此外,如果他更好地重构代码,他根本不需要打电话给forceUpdate

标签: javascript reactjs fetch-api


【解决方案1】:

要让您的代码正常工作,您可以尝试在服务中执行此操作:

obj.setState.bind(obj)( { data: json });

但是,无需将组件对象传递给您的服务。可以说,这不是一个好主意,因为您只是在不需要时耦合事物。让组件调用您的服务,然后决定如何处理数据:

 getOperatorsList: function () {
    return fetch( 'http://x.x.x.x/operators.list', {
        method: 'GET',
        credentials: 'include'
    }).then( function ( response ) {
        return response.json()
    }).then( function ( json ) {
        if (json.statusCode === 3 ) {
            cookieService.unsetCookie( 'sessId' );
        }
        return json;
    }).catch( function ( ex ) {
      console.log( 'parsing failed', ex );
    })

}

然后在你的组件中:

componentDidMount: function () {
    operatorsService.getOperatorsList()
    .then(function (json) {
       this.setState({ data: json });
    }.bind(this))
}

【讨论】:

  • 你忘记在getOperatorsList返回承诺
  • 我收到'Uncaught TypeError: Cannot read property 'then' of undefined' 并且我引用的问题在 fetch() 之前添加了一个返回值,但这也不起作用。我得到了和以前一样的行为。
  • @Harmeko 抱歉,我只是忘了返回承诺,我用 return 声明更新了 getOperatorsList。
  • 还是不行。我真的不明白怎么了。还有更多,当我尝试在您的then() 中使用console.log(this); 并查看状态时,其中有数据,但如果我console.log(this.state.data) 则没有。
  • @Harmeko 来自 React 文档:facebook.github.io/react/docs/component-api.html "setState() 不会立即改变 this.state 而是创建一个挂起的状态转换。调用此方法后访问 this.state 可能会返回现有值。”这就是为什么 console.log(this.state) 不向您显示数据的原因。如果还是不行,请尝试在render中调试/console.log(this.state.data)看看会发生什么。
猜你喜欢
  • 2014-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-14
  • 2017-11-25
  • 1970-01-01
  • 1970-01-01
  • 2020-07-03
相关资源
最近更新 更多