04 July 2018

ReactJS + Redux: Basic redux console

Add liblary
npm install
Basic redux console
ReatcJS 2018
import { createStore } from "redux";

//Reducer lắng nghe các action gọi đến từ dispatch
export function reducer(state, action) {
    switch (action.type) {
        case "INC":
            return state + action.payload;
        case "DEC":
            return state - action.payload;
        default: {
            return state;
        }
    }
}
//Tạo mới store nội dung là function reducer
const store = createStore(reducer, 0);
//Đăng ký store
store.subscribe(() => {
 console.log("store changed", store.getState())
})
//dispatch action
store.dispatch({type: "INC", payload: 2});
store.dispatch({type: "INC", payload: 232});
store.dispatch({type: "INC", payload: 65672});
store.dispatch({type: "DEC", payload: 27980780});

ReactDOM.render(
    <App />,
   document.getElementById('root'));
npm start

Result

0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang