1:Redux
集中状态管理工具,可以独立运行。
- 使用步骤
1.定义一个reducer函数(根据当前想要做的修改返回一个新的状态)
2.使用createStore方法传入reducer函数,生成一个store实例对象
3.使用store实例的subscribe方法订阅数据的变化(数据一旦变化,可以得到通知)
4.使用store实例的dispatch方法提交action对象触发数据变化(告诉reducer你想怎么改数据)
5.使用store实例的getState方法获取最新的状态数据更新到视图中。
//第一步:
function reducer(state = {count:0},action){
if(action.type ==='increment'){
return {count:state.count+1}
}else if(action.type ==='decrement'){
return {count:state.count+1}
}else{
return state
}
}
//第二步
const store = Redux.createStore(reducer);
//第三步:state变化的时候自动执行
store.subscribe(()=>{
console.log(store.getState());
document.getElementById('count').innerText =store.getState().count
})
//第四步
const inBtn = document.getElementById('increment');
inBtn.addEventListener('click',()=>{
store.dispatch({type:'increment'})
})
const deBtn = document.getElementById('decrement');
deBtn.addEventListener('click',()=>{
store.dispatch({type:'decrement'})
})
【信息由网络或者个人提供,如有涉及版权请联系COOY资源网邮箱处理】
© 版权声明
本平台(www.cooy.cn)的一切软件、教程及内容信息仅限用于学习和研究,付费仅为收集整理归类费用;
不得将上述内容用于商业或者非法用途,否则一切后果用户自行承担负责。本平台资源、内容、信息均来自来自用户上传,版权争议及其他问题与本平台无关。
您必须在下载后的24个小时之内从您的电脑或手机中彻底删除上述下载内容,如果您喜欢该程序或内容,请支持正版以获取更好的服务。我们非常重视版权问题,如有侵权请发送邮件至下方邮件(655465@qq.com),敬请谅解!
如发现违法违规内容,请联系下方邮箱举报,我们收到后将会第一时间处理。
THE END
暂无评论内容