Extract Local State into Redux/or help!

Tell us what’s happening:

Your code so far


// Redux:
const ADD = 'ADD';

const addMessage = (message) => {
return {
  type: ADD,
  message: message
}
};

const messageReducer = (state = [], action) => {
switch (action.type) {
  case ADD:
    return [
      ...state,
      action.message
    ];
  default:
    return state;
}
};

const store = Redux.createStore(messageReducer);

// React:
const Provider = ReactRedux.Provider;
const connect = ReactRedux.connect;

// Change code below this line
class Presentational extends React.Component {
constructor(props) {
  super(props);


this.state = {
  input: ''
}

this.handleChange = this.handleChange.bind(this);
this.submitMessage = this.submitMessage.bind(this);
}
handleChange(event) {
  this.setState({
    input: event.target.value
  });
}
submitMesssage() {

  this.props.submitNewMessage(this.state.input);
  this.setState({
    input: ''
  });
}
render() {
  return (
    <div>
    <h2>Type in a new Message:</h2>
    <input 
    value={this.state.input}
    onChange={this.handleChange}/><br/>
    <button onClick={this.submitMessage}>Submit</button>
    <ul>
    {this.props.messages.map( (message, idx) => {
      return (
        <li key={idx}>{message}</li>
      )
    })
    }
    </ul>
    </div>
  );
}
};

const mapStateToProps = (state) => {
return {messages: state}
};

const mapDispatchToProps = (dispatch) => {
return {
  submitNewMessage: (message) => {
    dispatch(addMessage(message))
  }
}
};
const Container = connect(mapStateToProps, mapDispatchToProps)(Presentational);

class AppWrapper extends React.Component {
render() {
  return (
    <Provider store={store}>
    <Container/>
    </Provider>
  );
}
};

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:76.0) Gecko/20100101 Firefox/76.0.

Challenge: Extract Local State into Redux

Link to the challenge:

Hey guys! I’ve been working on this for some time and frankly, I’m stuck. Can some one help? Thank you!

Normally switches have a break value, idk redux but just in case it helps. Now that I think, it’s probably when you do not return a value. :smile:

1 Like

Hello there,

I am not sure when you made the typo but there is one here:

submitMesssage() {

Only reason I noticed it is because this was driving me crazy…so, I put it through a diff checker.

1 Like

You also get this in the console which should help point you in the right direction.

TypeError: Cannot read property ‘bind’ of undefined

Just in case it isn’t clear submitMessage not submitMesssage

1 Like

Thank you!!! My fast typing will be the death of me!