33
loading...
This website collects cookies to deliver better user experience
react-redux
to connect the Redux store with your component, then extracting the local state into the Redux store.
class DisplayMessages extends React.Component {
render() {
return <div />
}
};
input
, that is set to an empty string and messages
set to an empty array.
class DisplayMessages extends React.Component {
constructor(props) {
super(props);
this.state = {
input: '',
messages: []
}
}
render() {
return <div />
}
};
DisplayMessages
component.render()
method have the component render an input
element, button
element, and ul
element.input
element changes, it should trigger a handleChange()
method. Also the input
element should render the value of input that's in the component's state. The button
element will trigger a submitMessage()
method when it's clicked.handleChange()
method should update the input
with what the user is typing and the submitMessage()
should concatenate the current message(stored in input
) to the messages
array in local state, and clear the value of the input
.ul
we need to map over the array of messages
and render it to the screen as a list of li
elements.class DisplayMessages extends React.Component {
constructor(props) {
super(props);
this.state = {
input: '',
messages: []
}
}
// Add handleChange() and submitMessage() methods here
render() {
return (
<div>
<h2>Type in a new Message:</h2>
{ /* Render an input, button, and ul below this line */ }
{ /* Change code above this line */ }
</div>
);
}
};
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,
messages: this.state.messages
})
}
submitMessage() {
this.setState({
input: '',
messages: [...this.state.messages, this.state.input]
})
}
render() {
return (
<div>
<h2>Type in a new Message:</h2>
<input onChange={this.handleChange} value={this.state.input}></input>
<button onClick={this.submitMessage}>Submit</button>
<ul>{this.state.messages.map(l => <li>{l}</li>)}</ul>
</div>
);
}
};
state
into Redux.ADD
and set it to a const ADD
. Then, define an action creator addMessage()
which creates the action to add a message. We need to pass a message to this action creator and include the message in the returned action. With it return an object with type
equal to ADD
and message
equal to the message that is passed in.Now they want us to create a reducer called messageReducer()
that handles the state for the messages. The initial state should equal an empty array. This reducer should add a message to the array of messages held in state, or return the current state. Finally, create your Redux store and pass it the reducer.
Answer:
const ADD = 'ADD'
function addMessage(message) {
return {
type: 'ADD',
message
}
}
const messageReducer = (intialState = [], action) => {
switch(action.type) {
case 'ADD':
return [...intialState, action.message];
default:
return intialState;
}
}
const store = Redux.createStore(messageReducer)