FilterableProductTable
(orange): contains the entirety of the exampleSearchBar
(blue): receives all user inputProductTable
(green): displays and filters the data collection based on user inputProductCategoryRow
(turquoise): displays a heading for each categoryProductRow
(red): displays a row for each productProductTable
, you’ll see that the table header (containing the “Name” and “Price” labels) isn’t its own component. This is a matter of preference, and there’s an argument to be made either way. For this example, we left it as part of ProductTable
because it is part of rendering the data collection which is ProductTable
’s responsibility. However, if this header grows to be complex (e.g., if we were to add affordances for sorting), it would certainly make sense to make this its own ProductTableHeader
component.FilterableProductTable
SearchBar
ProductTable
ProductCategoryRow
ProductRow
FilterableProductTable
) or with the ones lower in it (ProductRow
). In simpler examples, it’s usually easier to go top-down, and on larger projects, it’s easier to go bottom-up and write tests as you build.render()
methods since this is a static version of your app. The component at the top of the hierarchy (FilterableProductTable
) will take your data model as a prop. If you make a change to your underlying data model and call ReactDOM.render()
again, the UI will be updated. You can see how your UI is updated and where to make changes. React’s one-way data flow (also called one-way binding) keeps everything modular and fast.ProductTable
needs to filter the product list based on state and SearchBar
needs to display the search text and checked state.FilterableProductTable
.FilterableProductTable
FilterableProductTable
. First, add an instance property this.state = {filterText: '', inStockOnly: false}
to FilterableProductTable
’s constructor
to reflect the initial state of your application. Then, pass filterText
and inStockOnly
to ProductTable
and SearchBar
as a prop. Finally, use these props to filter the rows in ProductTable
and set the values of the form fields in SearchBar
.filterText
to "ball"
and refresh your app. You’ll see that the data table is updated correctly.FilterableProductTable
.value
prop of the input
to always be equal to the state
passed in from FilterableProductTable
.FilterableProductTable
will pass callbacks to SearchBar
that will fire whenever the state should be updated. We can use the onChange
event on the inputs to be notified of it. The callbacks passed by FilterableProductTable
will call setState()
, and the app will be updated.