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

javascript - Updating state after async action in react-redux - Stack Overflow

programmeradmin1浏览0评论

The general idea is that someone clicks the button, action fetches data from server and dispatches it. The state is updating and the content on the page is changing. The action looks exacly like that:

     export function getPosts(page){
     return function(dispatch){
      axios.get(`${ROOT_URL}/home?page=${page}`)
          .then(response => {
            dispatch ({
            type: FETCH_POSTS,
            payload: response.data        
          })
          })
          .catch((error) => {
            console.log(error)
          });
    }
    }

Reducer is pretty straightforward:

      export default function(state={}, action){
      switch(action.type){
        case FETCH_POSTS:
        return {posts: action.payload, ...state};
      }
      return state;
    }

And the main page is looking just like that:

    import React, { Component } from 'react';
    import * as actions from '../actions';
    import RequireAuth from './auth/require_auth';
    import { connect } from 'react-redux';
    import { pose } from 'redux';
    class Home extends Component {

    constructor(props) {
      super(props);
      this.state = {
        posts: 'loading....',
      };
      this.props.getPosts();
    }  
    handlePage(){
    console.log(this.props);
    let page = 3;
    this.props.getPosts();
    }

    ponentWillReceiveProps(nextProps){
      let posts = nextProps.posts.posts.map((post) => {
      return (<li key={post._id}>{post.date}</li>)
      });
      this.setState({posts: posts});

    }
    shouldComponentUpdate(nextState, nextProps){
    console.log(nextProps.posts, nextState.posts);
    return true;
    }
      render(){
        return(
          <div>
            {this.state.posts}
            <button onClick={this.handlePage.bind(this)}>change something</button>
          </div>
          )
    }
    }

    function mapStateToProps(state){
      return {posts: state.post}
    }

    export default connect(mapStateToProps, actions)(Home);

I was thinking that the state will update in ponentWillReciveProps but that is not happening. The data will be fetched after some time so i can't set state just like that. Any idea how to do this ?

The general idea is that someone clicks the button, action fetches data from server and dispatches it. The state is updating and the content on the page is changing. The action looks exacly like that:

     export function getPosts(page){
     return function(dispatch){
      axios.get(`${ROOT_URL}/home?page=${page}`)
          .then(response => {
            dispatch ({
            type: FETCH_POSTS,
            payload: response.data        
          })
          })
          .catch((error) => {
            console.log(error)
          });
    }
    }

Reducer is pretty straightforward:

      export default function(state={}, action){
      switch(action.type){
        case FETCH_POSTS:
        return {posts: action.payload, ...state};
      }
      return state;
    }

And the main page is looking just like that:

    import React, { Component } from 'react';
    import * as actions from '../actions';
    import RequireAuth from './auth/require_auth';
    import { connect } from 'react-redux';
    import { pose } from 'redux';
    class Home extends Component {

    constructor(props) {
      super(props);
      this.state = {
        posts: 'loading....',
      };
      this.props.getPosts();
    }  
    handlePage(){
    console.log(this.props);
    let page = 3;
    this.props.getPosts();
    }

    ponentWillReceiveProps(nextProps){
      let posts = nextProps.posts.posts.map((post) => {
      return (<li key={post._id}>{post.date}</li>)
      });
      this.setState({posts: posts});

    }
    shouldComponentUpdate(nextState, nextProps){
    console.log(nextProps.posts, nextState.posts);
    return true;
    }
      render(){
        return(
          <div>
            {this.state.posts}
            <button onClick={this.handlePage.bind(this)}>change something</button>
          </div>
          )
    }
    }

    function mapStateToProps(state){
      return {posts: state.post}
    }

    export default connect(mapStateToProps, actions)(Home);

I was thinking that the state will update in ponentWillReciveProps but that is not happening. The data will be fetched after some time so i can't set state just like that. Any idea how to do this ?

Share Improve this question edited Jan 29, 2017 at 19:05 madhurgarg 1,3412 gold badges11 silver badges18 bronze badges asked Jan 29, 2017 at 13:45 tusciortuscior 992 silver badges8 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 3

for async actions, there are two possible ways:

  1. an additional status flag
  2. additional actions

The workflow would like:

  1. Fetching data, dispatch an action, which sets status to 'fetching'
  2. if fetching is successfull, dispatch an action, which sets statusto 'success'
  3. if fecthing fails, dispatch an action, which sets status to 'error'

Further reading: Redux :: Async actions

发布评论

评论列表(0)

  1. 暂无评论