最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Accessing state in react - Stack Overflow

programmeradmin0浏览0评论

I have a function:

    getCommits: function() {
    fetch('/mits').then((response) => {
        if (response.status >= 400) {
            throw new Error("Bad response from server");
        }
        return response.json();
     })
     .then((mits) => {
       this.setState({mits: mits});
     });
  },

I set the state in this function and now I want other functions to access this state. How can I do this?

this.statemits was one idea I had.

     getTodaysCommits: function(){
     fetch('/mits').then((response) => {
        if (response.status >= 400) {
            throw new Error("Bad response from server");
        }
        return response.json();
     })
     .then((mits) => {
       var todaysCommits = [];
       for (var i = 0; i < mits.length; i++) {
         var today = new Date().toDateString();
         var stringToDate = new Date(mits[i]mit.author.date).toDateString();
         if (today == stringToDate){
           todaysCommits.push(mits[i])
         }
       }
       return todaysCommits;
     })
     .then((todaysCommits) => {
       this.setState({mits: todaysCommits});
     })
  },

In this function how can I use mits without having to fetch it from api and just use the state set in the previous function?

ponentDidMount: function() {
    this.getCommits();}

I have my function that gets set on ponentDidMount

I have a function:

    getCommits: function() {
    fetch('/mits').then((response) => {
        if (response.status >= 400) {
            throw new Error("Bad response from server");
        }
        return response.json();
     })
     .then((mits) => {
       this.setState({mits: mits});
     });
  },

I set the state in this function and now I want other functions to access this state. How can I do this?

this.state.mits was one idea I had.

     getTodaysCommits: function(){
     fetch('/mits').then((response) => {
        if (response.status >= 400) {
            throw new Error("Bad response from server");
        }
        return response.json();
     })
     .then((mits) => {
       var todaysCommits = [];
       for (var i = 0; i < mits.length; i++) {
         var today = new Date().toDateString();
         var stringToDate = new Date(mits[i].mit.author.date).toDateString();
         if (today == stringToDate){
           todaysCommits.push(mits[i])
         }
       }
       return todaysCommits;
     })
     .then((todaysCommits) => {
       this.setState({mits: todaysCommits});
     })
  },

In this function how can I use mits without having to fetch it from api and just use the state set in the previous function?

ponentDidMount: function() {
    this.getCommits();}

I have my function that gets set on ponentDidMount

Share Improve this question edited Oct 13, 2016 at 10:43 The worm asked Oct 13, 2016 at 10:23 The wormThe worm 5,88814 gold badges40 silver badges51 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

First you need to make sure that your state is set and then you can access it by referring to the context state as this.state.mits.

You need to make sure you are referring to the correct context.

Use it in the function as

getTodaysCommits: function(){

    var mits = this.state.mits;
    console.log(mits);

}
发布评论

评论列表(0)

  1. 暂无评论