मैं चारों ओर 'simplest-redux-example' on github साथ खिलवाड़ कर रहा हूँ और मैं एक दूसरे कम करने कि state.count decrements जोड़ दिया है आरंभ। यदि स्विच स्विच स्टेटमेंट में मेरे पास वृद्धि और कमी reducers है, तो यह ठीक काम करता है। जिस अभ्यास को मैं करना चाहता था वह रेड्यूसर को जितना संभव हो उतना मॉड्यूलर टुकड़ों में विभाजित करना था। यह कोड एक त्रुटि बताता है कि गिनती अपरिभाषित है।Redux reducers ही राज्य कुंजी
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')
);
यह कहा गया कि क्या लाइन है कि में? काउंटर समारोह में लेट संख्या = state.count पर – zerkms
। – tells
और तुम 'console.log करने की कोशिश की (राज्य)' है? – zerkms