New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redux #10

Merged
merged 6 commits into from Nov 20, 2017

Conversation

Projects
None yet
1 participant
@brunolm
Owner

brunolm commented Nov 20, 2017

Wrap your root component with the Provider component

import { Provider } from 'react-redux';

<Provider store={store} key="provider">

The store is created with

import { createStore } from 'redux';

const store = createStore(reducers);

Reducers is the combined reducers of your application

import { combineReducers } from 'redux';
import { routerReducer as routing } from 'react-router-redux';

export default combineReducers({
  homeReducer,
  routing,
});

A reducer boils down to a function that takes a state and an action and return a new state

const homeReducer = (state = initialState, action) => {
  return {
    ...state,
    something: action.something,
};

Your root components now have to be exported with a decorator (or a function call), example with decorator:

const mapStateToProps = (state) => {
  return {
    ...state.homeReducer,
  };
};

const mapDispatchToProps = (dispatch: Dispatch<any>) => ({
  toggleLoading: (options) => dispatch({ type: homeTypes.loading, ...options }),
});

@(connect(mapStateToProps, mapDispatchToProps) as any)

Hints

You can call mockStore with a function like mockStore(() => mockState); and then modify mockState at any point to simulate different states of the application.

Be careful when testing a reducer with an undefined value, see 13c63de

@brunolm brunolm merged commit 7a5c2d2 into master Nov 20, 2017

0 of 2 checks passed

continuous-integration/travis-ci/pr The Travis CI build failed
Details
continuous-integration/travis-ci/push The Travis CI build failed
Details

@brunolm brunolm deleted the redux branch Nov 22, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment