I have been reading several documents and watching videos regarding React Redux, but since all of them are different I wasn't able to apply that knowledge to some real project.
I will try to enumarate the process in order to use React Redux together.
Directory Structuring
- project
- src
- components
- User
- index.js (Container component)
- page.js (Presentational component)
- User
- actions
- users.js
- index.js (exports actionCreators combination)
- reducers
- users.js
- index.js (exports reducer combination with combineReducers
- constants
- actionTypes.js
- services
- users.js
- index.js
- store.js
- public
- index.html
- components
- src
Redux Setup
- We create constants in
project/src/constants/actionTypes.js
:export const CREATE_USER = 'CREATE_USER'; export const DELETE_USER = 'DELETE_USER'; export const UPDATE_USER = 'UPDATE_USER';
We create actionCreators en
project/src/actions/users.js
y luego se combinan enproject/src/actions/index.js
:- users.js
import { CREATE_USER } from '../constants/actionTypes'; export default function createUser(user) { type: CREATE_USER, user }
- index.js
import { createUser } from './users'; export default { createUser }
We create reducers in
project/src/reducers/users.js
and they are combined inproject/src/reducers/index.js
usingcombineReducers()
:- users.js
import { CREATE_USER, UPDATE_USER, DELETE_USER } from '../constants/actionTypes'; import { createUser } from '../services/users'; const initialState = { name: '', password: '', email: '' } export default function users(state = initialState, action) { switch (action.type) { case CREATE_USER: state = createUser(action.user); return state; } }
- index.js
import users from './users'; export default combineReducers({ users })
We create store in
project/src/store.js
:import { createStore } from 'redux'; import reducers from './reducers'; export const store = createStore(reducers);
React Redux Setup
We wrap component application
<Provider>
inproject/src/index.js
:import React from 'react'; import ReactDOM from 'react-dom'; import { Provider } from 'react-redux'; import { store } from './store'; const Root = () => ( ` <Provider store={store}> <App /> </Provider> ` ) ReactDOM.render(Root, document.getElementById('root');
We transform component state to properties with
mapStateToProps
inproject/src/components/User/index.js
:import React, { Component } from 'react'; import { createUser } from '../../actions/users'; import Page from './page'; class User extends Component { render() { return <Page users={this.props.users} /> } } const mapStateToProps = state => ({ users: this.props.users // what is mapped here? }); const mapDispatchToProops = dispatch => ({ // what about here? }); export default connect(mapStateToProps, mapDispatchToProps)(User);
So, the question would be, is this React-Redux cycle well formed? What is missing or wrong?
mapStateToProps
should probably beusers: state.users
in this case and your action should justreturn action.user
.) However, I'm not sure what kind of answer you're looking for, SO is for solving specific issues, I think this is more suited for Code Review once you get your project going. – Yannick K