Javascript 如何在react native中的一次重复中添加多个计数器
我想添加多个计数器,如countA、countV、countR,并想在onPress上更新特定计数器。但是当我更新其中任何一个的时候。然后所有计数器更新 以下是我在App.js中尝试的内容Javascript 如何在react native中的一次重复中添加多个计数器,javascript,react-native,redux,react-redux,Javascript,React Native,Redux,React Redux,我想添加多个计数器,如countA、countV、countR,并想在onPress上更新特定计数器。但是当我更新其中任何一个的时候。然后所有计数器更新 以下是我在App.js中尝试的内容 const initialState={ countA:0, countV:0, countR:0, }; function reducer(state = initialState, action){ switch(action.type){ case "INCR
const initialState={
countA:0,
countV:0,
countR:0,
};
function reducer(state = initialState, action){
switch(action.type){
case "INCREMENTA":
return {
countA: state.countA + 1,
}
case "INCREMENTV":
return {
countV: state.countV + 1,
}
case "INCREMENTR":
return {
countR: state.countR+ 1,
}
default:
return state;
}
}
const store = createStore(reducer);
在screen.js中
incrementA = () => {
this.props.dispatch({type:"INCREMENTA"})
}
incrementV = () => {
this.props.dispatch({type:"INCREMENTV"})
}
incrementR = () => {
this.props.dispatch({type:"INCREMENTR"})
}
///here is onPress button
const mapStateToProps=(state)=>({
countA:state.countA,
countV:state.countV,
countR:state.countR
})
export default connect(mapStateToProps)(detailScreen);
请忽略它=我想添加多个计数器,如countA、countV、countR,并想在onPress上更新特定计数器。但是当我更新其中任何一个的时候。然后所有计数器更新。我想添加多个计数器,如countA、countV、countR,并想在onPress上更新特定计数器。但是当我更新其中任何一个的时候。然后更新所有计数器。在Redux reducers中,您必须返回整个新状态,因此您的代码应该是:
const initialState={
countA:0,
countV:0,
计数:0,
};
函数缩减器(状态=初始状态,动作){
开关(动作类型){
案例“增量A”:
返回{
……国家,
countA:state.countA+1,
}
案例“递增V”:
返回{
……国家,
countV:state.countV+1,
}
案例“递增者”:
返回{
……国家,
countR:state.countR+1,
}
违约:
返回状态;
}
}
const store=createStore(reducer);