[Redux] Store Methods: getState(), dispatch(), and subscribe()
and redux Store methods Dispatch
2023-09-14 08:59:20 时间
console.clear(); const counter = (state = 0, action) => { switch (action.type) { case 'INCREMENT': return state + 1; case 'DECREMENT': return state - 1; default: return state; } } // Create a store const {createStore} = Redux; // Store hold the state, can accept a reducer function const store = createStore(counter); let currentState = store.getState(); console.log(currentState); // 0 store.dispatch( {type: 'INCREMENT'} ); console.log(store.getState()); // 1 store.subscribe( () => { document.body.innerText = store.getState(); }); document.addEventListener('click', ()=>{ store.dispatch( {type: 'INCREMENT'} ); })
If we run we will 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': return state + 1; case 'DECREMENT': return state - 1; default: return state; } } // Create a store const {createStore} = Redux; // Store hold the state, can accept a reducer function const store = createStore(counter); let currentState = store.getState(); console.log(currentState); // 0 store.dispatch( {type: 'INCREMENT'} ); console.log(store.getState()); // 1 const render = ()=> { document.body.innerText = store.getState(); } render(); store.subscribe( render); document.addEventListener('click', ()=>{ store.dispatch( {type: 'INCREMENT'} ); })
相关文章
- 开机提示:DISK BOOT FAILURE,INSERT SYSTEM DISK AND PRESS ENTER无法开机的解决办法「建议收藏」
- MIT 6.828 操作系统工程 Lab5: File system, Spawn and Shell 笔记
- ORA-24065: propagation for QUEUE string and DESTINATION string already disabled ORACLE 报错 故障修复 远程处理
- ORA-26653: Apply string did not start properly and is currently in state string ORACLE 报错 故障修复 远程处理
- MySQL Error number: MY-010357; Symbol: ER_TZ_CANT_OPEN_AND_LOCK_TIME_ZONE_TABLE; SQLSTATE: HY000 报错 故障修复 远程处理
- ORA-01678: parameter string must be pairs of pattern and replacement strings ORACLE 报错 故障修复 远程处理
- Exploring the Advantages of Using Float and Oracle in Financial Operations(floatoracle)
- Exploring the Power and Versatility of SAR on the Linux Platform(sarlinux)
- Mastering Linux Scripting: Tips and Techniques(linux下脚本)
- How to Find the Maximum Value in a MySQL Table: Tips and Tricks(mysql表最大值)
- Oracle数据库中的AND查询(oracle and查询)
- Mysql中强大的AND运算符的使用方法探究(mysql中and的使用)
- MySQL中的AND和OR使用逻辑运算符优化查询语句(mysql中and与or)