【问题标题】:When a variable is updating in reactjs, And how to force the update当一个变量在 reactjs 中更新时,以及如何强制更新
【发布时间】:2017-05-03 14:26:01
【问题描述】:

我试图理解, 我知道当我在 console.log(pictures) 得到最终结果时

当我“console.log(JSON.stringify(pictures))”时,我得到了当前的那个。

为什么在记录第二个选项的那一刻,数组是空的 最后它得到了一个值,

如何强制同步 value 和 this.state

问题是当我最后发送时:

ImageList pics={this.state.picturesArr} />

发送的是空的

let pictures = [];
let urls =[];
let titles=[];
let nextpage ='';
let pageback='';

class App extends Component {

    constructor(props) {
        super(props);

        this.state = {
            picturesArr: [],
            urlArr: [],
            titleArr:[],
            nextPage:'',
            prevuisPage:''
        };

        this.loadSubreddit = this.loadSubreddit.bind(this);
    }

    loadSubreddit(subre) {

        reddit.hot(subre).limit(5).fetch(function(res) {
            console.log(res);
            nextpage = res.data.after.toString();
            // pageback = res.data.before.valueOf();

            for (let i = 0; i < res.data.children.length; i++){
                pictures.push(res.data.children[i].data.url);
                urls.push(res.data.children[i].data.permalink);
                titles.push(res.data.children[i].data.title);
            }
        });

        this.setState({
            picturesArr: pictures,
            urlArr: urls,
            titleArr: titles,
            nextPage: nextpage,
            prevuisPage: pageback
        }, () => {
            console.log(this.state)
        });

        console.log(pictures);
        console.log(JSON.stringify(pictures));
    }


    componentWillMount() {
        console.log('comp is mounting');
        this.loadSubreddit('cats');
    }

    render() {
        return (
            <div className="App">
            <div className="App-header">
              <img src={logo} className="App-logo" alt="logo" />
              <h2>Welcome to React</h2>
            </div>
                <Col sm={8} md={10} smOffset={2} mdOffset={1} >
                    <ImageList pics={this.state.picturesArr} />
                </Col>
            </div>
        );
    }
}

export default App;

【问题讨论】:

标签: javascript reactjs create-react-app


【解决方案1】:

在您提供的代码中,您在 api-call 回调之外调用 this.setState

您需要 (1) 将设置状态的代码移动到回调中,以及 (2) 将回调函数绑定到组件实例以访问其this.setState

固定的loadSubbreddit函数如下所示。

loadSubreddit(subre) {

    reddit.hot(subre).limit(5).fetch(function(res) {
        console.log(res);
        nextpage = res.data.after.toString();
        // pageback = res.data.before.valueOf();

        for (let i = 0; i < res.data.children.length; i++){
            pictures.push(res.data.children[i].data.url);
            urls.push(res.data.children[i].data.permalink);
            titles.push(res.data.children[i].data.title);
        }

        this.setState({
            picturesArr: pictures,
            urlArr: urls,
            titleArr: titles,
            nextPage: nextpage,
            prevuisPage: pageback
        }, () => {
            console.log(this.state)
        });

        console.log(pictures);
        console.log(JSON.stringify(pictures));
    }.bind(this));
}

【讨论】:

  • 正确。现在pictures 不必是全局变量。
  • 谢谢!所以只是为了确保我弄错了,在使用 this.setState 时,它​​需要在 api 回调中使用(我需要阅读更多关于它的内容),并且因为为内部函数创建了另一个执行上下文,我需要绑定“this”,所以它会指向现有的状态
  • 一般进行api调用时,需要等待响应才能存储。您也可以在 api-call 回调之外使用 setState,但如果您想等待 api-call 的响应,则不能。
猜你喜欢
  • 1970-01-01
  • 2018-01-14
  • 1970-01-01
  • 2019-05-02
  • 2021-04-25
  • 2011-04-21
  • 1970-01-01
  • 1970-01-01
  • 2018-03-31
相关资源
最近更新 更多