(精华)2020年7月28日 React redux的使用
发布日期:2021-06-29 15:08:25
浏览次数:3
分类:技术文章
本文共 5615 字,大约阅读时间需要 18 分钟。
action-type.js
export const INCREMENT = 'increment'export const DECREMENT = 'decrement'
actions.js
import { INCREMENT,DECREMENT} from './action-type'export const increment = number => ({ type:INCREMENT,number})export const decrement = number => ({ type:DECREMENT,number})export const incrementAsync= ()=>{ return dispatch=>{ setTimeout(()=>{ const comments = 2 dispatch(increment(comments)) },1000) }}
reducers.js
import { INCREMENT,DECREMENT} from './action-type'export function counter(state=0,action){ switch(action.type){ case INCREMENT: return state + action.number case DECREMENT: return state - action.number default: return state }}
第一版本
import React from 'react'import ReactDOM from 'react-dom'import { createStore} from 'redux'import App from './components/app'import { counter} from './redux/reducers'const common = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__const store = createStore(counter,common())const render = ()=>{ ReactDOM.render(, document.getElementById('root'))}// 初始化渲染render()// 注册监听store.subscribe(render)
import React, { Component} from 'react'import * as actions from '../redux/actions'export default class App extends Component { state = { count: 0 } increment = () => { const num = this.refs.numSelect.value*1 this.props.store.dispatch(actions.increment(num)) } decrement = () => { const num = this.refs.numSelect.value*1 this.props.store.dispatch(actions.decrement(num)) } incrementIfOdd = () => { let count = this.state.count if(count%2==1) { this.props.store.dispatch(actions.increment(count)) } } incrementAsync = () => { const num = this.refs.numSelect.value*1 setTimeout(() => { this.props.store.dispatch(actions.increment(num)) }, 1000) } render () { return () }}click {
this.props.store.getState()} times { ' '} { ' '} { ' '} { ' '} { ' '}
第二版本
import React from 'react'import ReactDOM from 'react-dom'import { createStore} from 'redux'import { Provider} from 'react-redux'import App from './container/app.js'import { counter} from './redux/reducers'import { composeWithDevTools} from 'redux-devtools-extension'// const common = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__const store = createStore(counter,composeWithDevTools())ReactDOM.render(, document.getElementById('root'))
import React, { Component} from 'react'import { connect} from 'react-redux'import Counter from '../components/counter'import { increment,decrement} from '../redux/actions'export default connect( state=>({ count:state}), { increment, decrement })(Counter)
import React, { Component} from 'react'export default class Counter extends Component { increment = () => { const num = this.refs.numSelect.value*1 this.props.increment(num) } decrement = () => { const num = this.refs.numSelect.value*1 this.props.decrement(num) } incrementIfOdd = () => { const num = this.refs.numSelect.value*1 let count = this.props.count if(count%2==1) { this.props.increment(num) } } incrementAsync = () => { const num = this.refs.numSelect.value*1 setTimeout(() => { this.props.increment(num) }, 1000) } render () { return () }}click {
this.props.count} times { ' '} { ' '} { ' '} { ' '} { ' '}
第三版本
import React from 'react'import ReactDOM from 'react-dom'import { createStore,applyMiddleware} from 'redux'import { Provider} from 'react-redux'import App from './container/app.js'import { counter} from './redux/reducers'import { composeWithDevTools} from 'redux-devtools-extension'import thunk from 'redux-thunk'// const common = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__const store = createStore( counter, composeWithDevTools(applyMiddleware(thunk)))ReactDOM.render(, document.getElementById('root'))
import React, { Component} from 'react'import { connect} from 'react-redux'import Counter from '../components/counter'import { increment,decrement,incrementAsync} from '../redux/actions'export default connect( state=>({ count:state}), { increment, decrement, incrementAsync })(Counter)
import React, { Component} from 'react'export default class Counter extends Component { increment = () => { const num = this.refs.numSelect.value*1 this.props.increment(num) } decrement = () => { const num = this.refs.numSelect.value*1 this.props.decrement(num) } incrementIfOdd = () => { const num = this.refs.numSelect.value*1 let count = this.props.count if(count%2==1) { this.props.increment(num) } } incrementAsync = () => { const num = this.refs.numSelect.value*1 this.props.incrementAsync(num) } render () { return () }}click {
this.props.count} times { ' '} { ' '} { ' '} { ' '} { ' '}
转载地址:https://codeboy.blog.csdn.net/article/details/107648629 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
路过按个爪印,很不错,赞一个!
[***.219.124.196]2024年04月18日 17时03分48秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
这是美国MarTech最大的一家独立公司:HubSpot
2019-04-29
从开发到产出:关于机器学习的七则干货建议
2019-04-29
你想成为数据科学家吗?不要把机器学习当成入门第一课
2019-04-29
你想成为数据科学家吗?不要把机器学习当成入门第一课
2019-04-29
现代社会悖论:信息泛滥是一只不守规矩的野兽
2019-04-29
如何设计自己的第一个加密交易机器人?
2019-04-29
浪费在Excel上的时间:如何开始专家式机器学习实验追踪?
2019-04-29
失业三星期:我寻找第二份编程工作之路
2019-04-29
跳过媒介,我们能不能只用思想控制计算机?
2019-04-29
服务器宕机:谷歌最近经历了“黑客攻击”吗?
2019-04-29
RepVGG:极简架构,SOTA性能,让VGG式模型再次伟大
2019-04-29
提升查询技能,这7条SQL查询错误必须解决
2019-04-29
TKDE 2020 | 综述:基于知识图谱的推荐系统
2019-04-29
休息时间!哪些业余活动能提升开发人员的技能?
2019-04-29
把人脑“森林”上传到计算机:人类脑计划只是一个科幻野心?
2019-04-29
智力测试:如何超越动物之智?
2019-04-29
事关人类生存?为什么要探寻AI系统的可解释性?
2019-04-29
安全工程师必知:常见Java漏洞有哪些?
2019-04-29
数据科学中的计量经济学技术
2019-04-29
突破边界:数据科学、数据工程和技术的未来
2019-04-29