// Trong file App.js import { createStore } from 'redux'; import {status, sort} from './../actions/index'; var initialState = { status: false, sort: { by: 'name', value: -1 } } var myReducer = (state = initialState, action) => { if(action.type === 'TOOGLE_STATUS') { state.status = !state.status } if(action.type === 'SORT') { state.sort = { by: action.sort.by, value: action.sort.value } } return state; } const store = createStore(myReducer); console.log('Default',store.getState()); store.dispatch(status()); console.log('TOOGLE_STATUS',store.getState()); store.dispatch(sort({ by: 'sort', value: 1 })); console.log('SORT',store.getState());
// Trong folder/fiel action/index.js export var status = () => { return { type: 'TOOGLE_STATUS' } } export var sort = (sort) => { return { type: 'SORT', sort: sort } } Điểm hạn chế của cái này: value 1 Default, TOOGLE_STATUS, SORT đều có value :1 :(
Last updated 5 years ago
Was this helpful?