javascript React-Redux文件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript React-Redux文件相关的知识,希望对你有一定的参考价值。

## Dependencies

- redux
- react-redux
- redux-thunk
- redux-logger
- redux-immutable-state-invariant
- axios

```
yarn add redux react-redux redux-thunk redux-logger redux-immutable-state-invariant axios
```
import axios from 'axios';
import * as types from '../datas/actionTypes';
import { api } from '../config';
const API = api[process.env.NODE_ENV];

// POPULAR
export function retrieveDefaultSuccess(res) {
  return {
    type: types.RETRIEVE_DEFAULT_SUCCESS,
    default: res,
  };
}

export function retrieveDefault() {
  return function(dispatch) {
    return axios
      .get(`${API}/__default__`)
      .then(res => {
        dispatch(retrieveDefaultSuccess(res));
      })
      .catch(error => {
        console.log('Default', error);
      });
  };
}
export const RETRIEVE_DEFAULT_SUCCESS = 'RETRIEVE_DEFAULT_SUCCESS';
import * as types from '../datas/actionTypes';
import initialState from './initialState';

export default function(state = initialState.default, action) {
  switch (action.type) {
    case types.RETRIEVE_DEFAULT_SUCCESS:
      return {
        ...state,
        default: action.default,
      };
    default:
      return state;
  }
}
export default {
  default: {},
};
import { createStore, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
import logger from 'redux-logger';
import reducer from '../reducers';

let middleware = [thunk];

// Remove the `true` part in production
if (true) {
  const reduxImmutableStateInvariant = require('redux-immutable-state-invariant').default();
  middleware = [...middleware, reduxImmutableStateInvariant, logger];
} else {
  middleware = [...middleware];
}

export default function configureStore(initialState) {
  return createStore(reducer, initialState, applyMiddleware(...middleware));
}

以上是关于javascript React-Redux文件的主要内容,如果未能解决你的问题,请参考以下文章

javascript 尝试用`render` prop替换react-redux的`connect` HoC。

React之React-redux数据流转流程

React-Redux与Vuex使用对比

找不到模块“react-redux”的声明文件。

react-redux 类型文件中的 TypeScript 错误

Redux及React-Redux概述