In React, data travels two directions: top-down in the form of state propagating throughout controls, and bottom-up as interacting with the UI flows back up to modify the state. When writing an application it's often helpful to think of these two directions as separate parts of the development process.
Step #3 of "Thinking in React" suggests finding the "minimal set of mutable state" that your application requires. So in this demo we are going to add that "minimal state" to our application and drive our UI off of that data. With that done, the next step will be to create ways to modify that state, which will in turn cascade down through our UI. This reconciliation process, figuring out what in your UI needs to change based on changing state, is what React excels at.
For our minimal state, we're going to include just two keys: todos
and filter
. We don't need to worry about a remaining
value because we can calculate that by looking at the number of unchecked todos.
So here is our full constructor:
constructor(props) {
super(props);
this.state = {
todos: {
'04': {
label: 'Todo 4',
completed: true
},
'03': {
label: 'Todo 3',
completed: false
},
'02': {
label: 'Todo 2',
completed: false
},
'01': {
label: 'Todo 1',
completed: false
}
},
filter: 'all'
};
}
You could also use an array to represent your todos. Array manipulation can be easier in some cases, but this object approach simplifies other functionality and will ultimately be more performant.
To avoid reaching into state over and over, we once again use destructuring to pull out the pieces we need.
const { filter, todos = [] } = this.state;
Note that I've set
todos
to default to an empty array so that thetodos
variable is never undefined
Now we can pass filter
and todos
into our components.
return (
<div>
<TodoHeader filter={filter} />
<TodoList todos={todos} filter={filter} />
<TodoFooter todos={todos} />
</div>
);
I've already pulled out our props into filter
and todos
variables, and written a bit of JS that will return an array of filtered todo id
s. We'll be using that filtered array to render our todo items.
{
filteredTodos.map(id => <TodoListItem key={id} id={id} {...todos[id]} />);
}
map
: This method transforms the array it's called on into a new array (our rendered TodoListItems).key
: We use theid
from thefilterTodos
array as the list item key to help React track each item as state changes and the component re-renders.id
: Thekey
is not actually passed into the component, so we pass the same value asid
as well. This will help us out later.todos[id]
: Lastly we use theid
to grab the todo from ourtodos
object, then use the spread operator to pass through the todo'slabel
andcompleted
values.This spread operator is the same as saying
label={todos[id].label} completed={todos[id].completed}
. Pretty obvious why spread is so handy!
Within the header we've got a situation where we not only want to pass filter
state down to it, but we also want to maintain state within the control. Fortunately, this is no problem at all for React. First off let's deal with the incoming state.
In CSS-based styling, visual states are applied by adding and removing classes. We can use the filter value to conditionally add a class, thereby lighting up the correct filter button.
<nav className="filter">
<button className={filter === 'all' ? 'selected' : ''}>all</button>
<button className={filter === 'active' ? 'selected' : ''}>active</button>
<button className={filter === 'completed' ? 'selected' : ''}>completed</button>
</nav>
The ternary operator
condition ? expressionIfTrue : expressionIfFalse
is widely used in React code, as each expression could be a string for a className or even a JSX element.
In React, form elements such as <input>
, <textarea>
, and <select>
can be used as either uncontrolled or controlled. (This paradigm also applies to UI Fabric's customized implementations of form components, which we'll use later.)
An uncontrolled input maintains its current value internally and updates it based on user interactions (entering text, choosing options, etc). The code only pulls the value from the input when it's needed, such as on submit. This is similar to how inputs in a plain HTML form work.
A controlled input takes its current value from a prop and use a callback to notify the parent component of changes made by the user. The input's value doesn't change until the parent component updates the input's props in response to the callback.
Typically, a controlled input's current value is stored in the parent component's state (then passed to the input as a prop during render). The parent updates its state in response to the callback, which causes the input to be re-rendered with a new prop value. This round trip process might sound inefficient, but in reality it has little to no impact and helps enable some advanced form functionality.
The distinction between controlled and uncontrolled is important to understand when writing or using form components, and misunderstandings of this concept are a very common source of bugs. See this article for a more detailed explanation.
To add a controlled input, we need two things, which our demo already provides:
- A state variable to hold the input's value:
this.state = { labelInput: '' };
- A callback function to update that value:
_onChange = evt => {
this.setState({ labelInput: evt.target.value });
};
With those two pieces in place, we can update our uncontrolled input to being controlled.
<input value={this.state.labelInput} onChange={this._onChange} className="textfield" placeholder="add todo" />
If you have React Dev Tools installed, open them up and take a look at labelInput as we type in the input.