console.clear (); Const counter= (state = 0, action) = = { Switch(action.type) { Case' INCREMENT ': returnState + 1; Case' Decrement ': returnState-1; default: returnState ; }} //Create a storeconst {CreateStore} =Redux;//Store hold the state, can accept a reducer functionConst STORE =createstore (counter); let CurrentState=store.getstate (); Console.log (currentstate);//0Store.dispatch ({type:' INCREMENT '}); Console.log (Store.getstate ()); //1Store.subscribe (()={Document.body.innerText=store.getstate ();}); Document.addeventlistener (' Click ', () ={store.dispatch ({type:' INCREMENT '} );})
If We run we'll miss the first init state, it starts from ' 2 ' ...
So we need-to-call it before subscribe:
console.clear (); Const counter= (state = 0, action) = = { Switch(action.type) { Case' INCREMENT ': returnState + 1; Case' Decrement ': returnState-1; default: returnState ; }} //Create a storeconst {CreateStore} =Redux;//Store hold the state, can accept a reducer functionConst STORE =createstore (counter); let CurrentState=store.getstate (); Console.log (currentstate);//0Store.dispatch ({type:' INCREMENT '}); Console.log (Store.getstate ()); //1const RENDER = () = {Document.body.innerText = store.getstate ();} Render (); Store.subscribe (render); Document.addeventlistener (' Click ', () ={store.dispatch ({type:' INCREMENT '} );})
[Redux] Store methods:getstate (), dispatch (), and subscribe ()