React Lesson 13 Part 2: Asynchronous actions

栏目: IT技术 · 发布时间: 5年前

内容简介:Hey everyone. In the previousarticle, we usedWe are not getting articles fromNow update the initial state:
React Lesson 13 Part 2: Asynchronous actions

React Lesson 13 Part 2: Asynchronous actions

Hey everyone. In the previousarticle, we used Redux Thunk and learned how to structure actions for asynchronous calls. Now we will update reducer to handle them and then dispatch them from  Articles.js . Let’s update the reducer first.

We are not getting articles from fixtures.js now. That means we will initially have an empty array in articles instead of normalizedArticles and defaultArticles will look something like this:

const defaultArticles = recordsFromArray(Article, []);

Now update the initial state:

const defaultState = new Map({
    isFetching: false,
    errors: new [],
    entities: defaultArticles,
});

Having the articles added here, we receive a structure that can be reused from one reducer to another, which means entities will contain articles and comments. Our code below will also be changed:

export default (state = defaultState, action) => {
    const { type, payload, response, randomId } = action;
    switch (type) {
        case ADD_COMMENT:
      return {
        ...state, entities: state.entities.updateIn(
          [action.payload.articleId, "comments"],
          comments => comments.concat(action.randomId)
        )};
    case DELETE_ARTICLE:
      return { ...state, entities: state.entities.delete(action.payload) };
 
    //Add the three cases below
    case FETCH_ARTICLE_REQUEST:
        return { ...state, isFetching: true };
    case FETCH_ARTICLE_FAILURE:
        return { ...state, isFetching: false, errors: state.errors.push(action.error) };
    case FETCH_ARTICLE_SUCCESS:
        return { ...state, isFetching: false, entities: recordsFromArray(Article, action.payload) };
    }
    return state
}

As you can see, we have slightly updated our INITIAL_STATE which means we have to update the containers where we are consuming this state. Let’s start with  Filters.js :

//just update mapStateToProps
 
const mapStateToProps = state => {
  return {
    articles: state.article.entities.valueSeq(),
    filters: state.filters
  };
};

Now update containers/Articles.js . We are going to call action where we are fetching all the articles from the API and update state to get those articles from entities:

/ add import
import { fetchArticleRequest } from '../actions';
 
//update component
class Articles extends React.Component {
  componentDidMount(){
    this.props.fetchArticleRequest();
  }
 
  render(){
    const { articles, isFetching } = this.props;
    if(isFetching) return <p>Loading...</p>;
    return <ArticleList articles={articles} />;
  }
}
 
//update mapStateToProps
const mapStateToProps = state => {
  return {
    isFetching: state.article.isFetching,
    articles: filterArticles(state.article.entities, state.filters)
  };
};
 
export default connect(
  mapStateToProps,
  { fetchArticleRequest } //connect action to store
)(Articles);

In the render function, we are checking isFetching flag which we defined earlier. While isFetching is true , we know the request is in process and we can show a loading text or spinner while we get our data. Once we have the data isFetching will be updated to false and render function will render the ArticleList on screen.

That’s it. We have successfully connected actions with reducer and can fetch articles from the API. I hope you understood how to use thunk for asynchronous calls in actions. Go ahead and check it. Lesson code can be found in the GitHub repo .


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

机器消灭秘密

机器消灭秘密

安迪•格林伯格 (Andy Greenberg) / 王崧、王涛、唐禾 / 重庆出版社 / 2017-8-10 / 49.8

《机器消灭秘密》一书中,格林伯格深入研究并生动再现了那些拥有全能技术的网络安全魔术师,他们将任何企图染指个人隐私的所谓国家机密的保密性打得粉碎。这本精心组织的著作是对此题材感兴趣的读者的必读之书,即便现在你可能不感兴趣,将来也极有可能希望了解这些内容,因为任何人都会不可避免地置身其中。无论你是初涉电脑屏幕之后的虚拟战场的新生,还是经验丰富的维基解密观察家,本书都是不可多得的上乘之作,你总会在其中发......一起来看看 《机器消灭秘密》 这本书的介绍吧!

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具