A
A
AVasiliev3222017-06-15 15:28:40
React
AVasiliev322, 2017-06-15 15:28:40

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);

/**reducer
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;
}

/**reducers
export default combineReducers({
  text
});

/**index.js
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

1 answer(s)
D
davidnum95, 2017-06-15
@AVasiliev322

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 question

Ask a Question

731 491 924 answers to any question