Add hints and solution for this challenge (#34430)
* Add hints and solution for this challenge * Add original challenge seed code to solution
This commit is contained in:
@ -3,8 +3,119 @@ title: Extract Local State into Redux
|
|||||||
---
|
---
|
||||||
## Extract Local State into Redux
|
## Extract Local State into Redux
|
||||||
|
|
||||||
This is a stub. <a href='https://github.com/freecodecamp/guides/tree/master/src/pages/certifications/front-end-libraries/react-and-redux/extract-local-state-into-redux/index.md' target='_blank' rel='nofollow'>Help our community expand it</a>.
|
### Hint 1
|
||||||
|
You need to change the following sections:
|
||||||
|
* default state declarations: remove `messages`
|
||||||
|
* `submitMessage`: use `props`
|
||||||
|
* `render`: the unordered list should use `props` instead of `this.state.messages`
|
||||||
|
|
||||||
<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>.
|
### Hint 2
|
||||||
|
Replace `this.state.messages` with `this.props.messages`.
|
||||||
|
|
||||||
<!-- The article goes here, in GitHub-flavored Markdown. Feel free to add YouTube videos, images, and CodePen/JSBin embeds -->
|
### Hint 3
|
||||||
|
The `submitMessage` function still needs to set the state of the `input`.
|
||||||
|
|
||||||
|
|
||||||
|
### Solution
|
||||||
|
<details>
|
||||||
|
<summary>Spoiler!</summary>
|
||||||
|
|
||||||
|
```jsx
|
||||||
|
// 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
|
||||||
|
});
|
||||||
|
}
|
||||||
|
submitMessage() {
|
||||||
|
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>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
// Change code above this line
|
||||||
|
|
||||||
|
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>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
</details>
|
||||||
|
Reference in New Issue
Block a user