Tell us what’s happening:
I have tried to pass this one but I’m stuck on these two tasks any help would be appreciated! Show example for corrections.
Your code so far
// Redux setup (assuming this part is already defined)
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 component (assuming this part is already defined)
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
});
}
submitMessage() {
// Notice we're calling submitMessage here, not submitNewMessage
this.props.submitMessage(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>
);
}
};
// React-Redux connection - The critical part to fix
const mapStateToProps = (state) => {
return {
messages: state
}
};
const mapDispatchToProps = (dispatch) => {
return {
submitMessage: (message) => {
dispatch(addMessage(message))
}
}
};
// Connect the components properly
const Container = ReactRedux.connect(
mapStateToProps,
mapDispatchToProps
)(Presentational);
// Wrap Container with Provider
class AppWrapper extends React.Component {
render() {
return (
<ReactRedux.Provider store={store}>
<Container />
</ReactRedux.Provider>
);
}
};
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.3 Safari/605.1.15
Challenge Information:
React and Redux - Connect Redux to the Messages App