Add hint and solution (#34429)

* Add hint and solution

* Add original challenge seed code to solution
This commit is contained in:
coderlyn
2019-03-30 01:38:07 +11:00
committed by Randell Dawson
parent 03c603787f
commit 7bc3f6378f

View File

@ -3,8 +3,95 @@ title: Use Provider to Connect Redux to React
---
## Use Provider to Connect Redux to React
This is a stub. <a href='https://github.com/freecodecamp/guides/tree/master/src/pages/certifications/front-end-libraries/react-and-redux/use-provider-to-connect-redux-to-react/index.md' target='_blank' rel='nofollow'>Help our community expand it</a>.
### Hint 1
<a href='https://github.com/freecodecamp/guides/blob/master/README.md' target='_blank' rel='nofollow'>This quick style guide will help ensure your pull request gets accepted</a>.
You do not need to wrap the `Provider` in any `<div>` tags.
<!-- The article goes here, in GitHub-flavored Markdown. Feel free to add YouTube videos, images, and CodePen/JSBin embeds -->
### Solution
```jsx
// Redux Code:
const ADD = 'ADD';
const addMessage = (message) => {
return {
type: ADD,
message
}
};
const messageReducer = (state = [], action) => {
switch (action.type) {
case ADD:
return [
...state,
action.message
];
default:
return state;
}
};
const store = Redux.createStore(messageReducer);
// React Code:
class DisplayMessages extends React.Component {
constructor(props) {
super(props);
this.state = {
input: '',
messages: []
}
this.handleChange = this.handleChange.bind(this);
this.submitMessage = this.submitMessage.bind(this);
}
handleChange(event) {
this.setState({
input: event.target.value
});
}
submitMessage() {
const currentMessage = this.state.input;
this.setState({
input: '',
messages: this.state.messages.concat(currentMessage)
});
}
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.state.messages.map( (message, idx) => {
return (
<li key={idx}>{message}</li>
)
})
}
</ul>
</div>
);
}
};
const Provider = ReactRedux.Provider;
class AppWrapper extends React.Component {
// Below is the code required to pass the test
render() {
return (
<Provider store={store}>
<DisplayMessages />
</Provider>
);
}
// Above is the code required to pass the test
};
```