首先定义state
redux函数中接受接受两个参数,第一个参数是state的值,第二个参数是action,、
在switch中来判断action.type的值,来进行选分支
以下实在index页面写的
const initstate = {
name:'张三',
age:20
}
****
**//定义reduce函数**
function count (state = initstate,action) {
// let {name} = state
console.log(action)
switch (action.type) {
case 'int1':
console.log(action.pload)
return {...state, ...{name:action.pload}}
default:
return state
}
}
const store = createStore(
count,
applyMiddleware(promiseMiddleware, thunk)
)
//定义store
const initstate = {
name:'张三',
age:20
}
定义store来进行调用readuce函数来进行修改state的值
store里面可以传两个参数,一个是promiseMiddleware,一个是thunk
**使用reduce的页面**
@connect(state => ({
name:state.name,
}),{
isName:()=>({type:"int2",pload:"小刚"})
})
定义@connect ,可以接受两个参数, 第一个数据是redux的state的值,第二个参数是定义方法和type的值,是组件中可以调用,根据此处的type值来进行switch周那个分支
在使用reduce的页面就可以使用
this.props.函数命来进行调用,正常的使用功能reduce里面的值了
//最后使用
ReactDOM.render(
//一定要使用Provider标签包裹,定义store的值
<Provider store={store}>
<App />
</Provider>
, document.getElementById('root'))
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。