Getting Started with React Redux
- Let's just go over what we have learned so far from FreeCodeCamps lessons. React is a view library that you provide with data, then it renders the view in an efficient way.
- Redux is a state management framework that you can use to simplify the management of your application's state.
- Basically, in a React Redux app, you create a single Redux store that manages the state of your entire app.
- Your React components subscribe to only the pieces of data in the store that are similar to their role. Then, you dispatch actions directly from React components, which then trigger store updates.
- Over the course of these next post/small helpful articles, we'll create a simple React component which allows you to input new text messages. Which are then added to an array that's displayed in the view.
- Next, we'll create a Redux store and actions that manage the state of the messages array. When all of that is set and done, we'll use
react-reduxto connect the Redux store with your component, then extracting the local state into the Redux store.
class DisplayMessages extends React.Component {
render() {
return <div />
}
};
- Here we start with a DisplayMessages component. We need to add a constructor to this component and initialize it with a state that has two properties:
input, that is set to an empty string andmessagesset to an empty array.
class DisplayMessages extends React.Component {
constructor(props) {
super(props);
this.state = {
input: '',
messages: []
}
}
render() {
return <div />
}
};
Manage State Locally First
- Here we'll finish creating the
DisplayMessagescomponent. - We first need to in
render()method have the component render aninputelement,buttonelement, andulelement. - When the
inputelement changes, it should trigger ahandleChange()method. Also theinputelement should render the value of input that's in the component's state. Thebuttonelement will trigger asubmitMessage()method when it's clicked. - Now, we should be writing these two methods. The
handleChange()method should update theinputwith what the user is typing and thesubmitMessage()should concatenate the current message(stored ininput) to themessagesarray in local state, and clear the value of theinput. - With the
ulwe need to map over the array ofmessagesand render it to the screen as a list oflielements. - FreeCodeCamp wants a lot but it's not too complicated.
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>
);
}
};
- Answer:
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>
);
}
};
Extract State Logic To Redux
- Now that we have finished the React Component, we need to move the logic it's performing locally in its
stateinto Redux. - This is the first step to connect the simple React app to Redux. The only thing our app can do at the moment is add new messages from the user to an unordered list.
- The instructions that freeCodeCamp has asked of us is actually quite easy.
- First, the want us to define an action type
ADDand set it to a constADD. Then, define an action creatoraddMessage()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 withtypeequal toADDandmessageequal 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)
Top comments (1)
Why using classes?
And why using the old redux boilerplate?