Answer the question
In order to leave comments, you need to log in
Why is the react-redux component not rendering?
Can't find an error. I created the simplest component, when the action button is clicked, the action fires, the store changes, but the props remain unchanged in the component. Why is this happening?
/**Text.js
class Text extends Component {
render () {
return(
<div>
<p>{this.props.state.date}</p>
<button onClick={this.props.onSelect}></button>
</div>
)
}
}
export default connect(
state => ({state: state.text}),
dispatch => ({
onSelect(date) {
dispatch({type: 'SELECT__DATE', payload: '1'});
}
})
)(Text);
const innitialState = {
date: '1'
}
export default function (state = innitialState, action) {
var newState = Object.assign(state);
switch (action.type) {
case 'SELECT__DATE':
newState.date += action.payload;
break;
}
return newState;
}
export default combineReducers({
text
});
const middleware = [thunk];
let store = createStore(reducers, applyMiddleware(...middleware));
ReactDOM.render(
<Provider store = {store}>
<Text />
</Provider>,
document.getElementById('root'));
Answer the question
In order to leave comments, you need to log in
Because Object.assign takes the target object as the first argument, not what you think. Need like this:
export default function (state = innitialState, action) {
switch (action.type) {
case 'SELECT__DATE':
return Object.assign({}, state, {
date: state.date + action.payload,
});
break;
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question