Tôi đang rối tung xung quanh với 'simplest-redux-example' on github và tôi đã thêm bộ giảm tốc thứ hai làm giảm state.count. Nếu tôi có các bộ giảm gia tăng và giảm trong câu lệnh case switch, nó hoạt động tốt. Bài tập tôi muốn thực hiện là chia nhỏ các phần nhỏ thành nhiều phần mô-đun nhất có thể. Mã này ném một lỗi nói rằng số lượng là không xác định.Bộ giảm tốc Redux khởi tạo cùng một khóa trạng thái
import React from 'react';
import { createStore, combineReducers } from 'redux';
import { Provider, connect } from 'react-redux';
// React component
class Counter extends React.Component {
render(){
const { value, onIncreaseClick, onDecreaseClick } = this.props;
return (
<div>
<span>{value}</span>
<button onClick={onIncreaseClick}>Increase</button>
<button onClick={onDecreaseClick}>Decrease</button>
</div>
);
}
}
// Action:
const increaseAction = {type: 'increase'};
const decreaseAction = {type: 'decrease'};
// Reducer:
function counter(state, action) {
let count = state.count;
switch(action.type){
case 'increase':
return {count: count+1};
default:
return state;
}
}
function decrementer(state, action) {
let count = state.count;
switch(action.type){
case 'decrease':
return {count: count -1};
default:
return state;
}
}
const rootReducer = combineReducers({
counter,
decrementer
})
// Store:
let store = createStore(rootReducer, {count: 0});
// Map Redux state to component props
function mapStateToProps(state) {
console.log("mapStatetoProps heyyyy");
return {
value: state.count
};
}
// Map Redux actions to component props
function mapDispatchToProps(dispatch) {
console.log("mapDispatchtoProps heyyyy");
return {
onIncreaseClick:() => dispatch(increaseAction),
onDecreaseClick:() => dispatch(decreaseAction)
};
}
// Connected Component:
let App = connect(
mapStateToProps,
mapDispatchToProps
)(Counter);
React.render(
<Provider store={store}>
{() => <App />}
</Provider>,
document.getElementById('root')
);
Tại những gì dòng nó khẳng định rằng? – zerkms
tại let count = state.count trong hàm đếm. – tells
Và bạn đã thử 'console.log (tiểu bang)' chưa? – zerkms