admin管理员组

文章数量:1305999

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);

}

本文标签: javascriptAccessing state in reactStack Overflow