Understanding Inverse Data Flow

This blog requires some base knowledge of Props and State in React. You can read up on both here.

What is Inverse Data Flow?

Up until this point, we have learned that the way data flows is from a parent component to a child component in t…


This content originally appeared on DEV Community and was authored by DEV Community

This blog requires some base knowledge of Props and State in React. You can read up on both here.

What is Inverse Data Flow?

Up until this point, we have learned that the way data flows is from a parent component to a child component in the form of a prop. With inverse data flow, we are switching that flow so the data is passed up from the child component to the parent component.

How Does It Work?

In order for a child component to pass up data up, the parent must pass down a callback function to the child. The callback function will be passed down as a prop to the child (like in regular data flow). By having the function defined in the parent component, we can invoke it in the child and then the parent will have access to the passed data.

Conceptualization

This is a pretty important concept in React and something that you can use all the time. Once you get the hang of it, you can use it for component reusability, cleaner code, rendering components and help with your events. Before we jump into actually writing out code to show how it works, let's break it down to a very simplistic concept, using a situation many may be familiar with outside of code. Take a look at this diagram and below we will walk through how it helps explain inverse data flow.

Image description

We have our parent (mom) and two of her children (Bob and Sally). Mom gives Bobby some toys (our state data) and Bobby now has access to those toys. Bob's sister wants access to toys as well so she asks Bob. Bob says no- this is important to note because Children cannot pass data to another child component. It must pass through the parent first and then to the child. Sally asks mom for some toys and mom goes to Bob, who has all the toys(data). She puts out her hand (callback function) and Bob hands her a toy(invoking callback and passing data back up to parent). Mom now has possession of the toy, and she could hold it and do something else with it, but instead she passes it back down to Sally (setting a new state). To put it into a hierarchical structure, it'll look like this:

Image description

Seeing it in Code

Now with that visualization of how data is passed, let's walk through an example of code.

We are starting with an App component (our parent), which has GroceryItemsList and GroceryCart as children components. We are keeping State in our App component (an array of groceryItems)and passing it down to GroceryItemsList, and then iterating over it so it creates a list of individual grocery items(an object). Our goal is to be able to click on a GroceryItem and have it added to our GroceryCart component. This is visualization of our data flow:

Image description

Let's start by making a callback function in our App component and passing that as a prop to our GroceryItemsList.

Passing as Prop

Now we need to go into our GroceryItemsList and destructure our prop, and pass it along to our GroceryItem. Of note: we are passing along our onGroceryItemClick callback function as a prop but naming it handleGroceryItemClick to make it clearer what it will be doing.

Passing as Prop Again

Now in our GroceryItemCard, we are going to take that callback prop and give it the data it needs to invoke it.

Invoking Callback

We now see that when we click on a GroceryItemCard it is console.logging:

We are adding this to GroceryCart
 {id:1, item:'Milk', price:'$3.99', brand:'Lucerne', image:'https://media.istockphoto.com/photos/kefir-milk-or-turkish-ayran-drink-are-poured-into-a-glass-cup-from-a-picture-id1198789194?b=1&k=20&m=1198789194&s=170667a&w=0&h=f85XhCzCWQhdFn0LaRckyOkJui88sYNoctN9zEmS4y0=' }

We have successfully passed down a callback function and invoked it from child to parent! That's inverse data flow!!

Now that the parent has the GroceryItem object, it can set the state of the GroceryCart to that object and render it.

Setting State of Other Child

And there we go- Inverse Data Flow. In my experience inverse data flow works really well for events (onChange, onClick, etc (especially search bars or any kind of filters)). It helps with component reusability and organizing where we put State in our library. It is a tricky concept to understand, but what's helped me is to make sure you name props appropriately and work from top to bottom, from parent to child, so you can keep track of what is being passed down.

SOURCE:
https://reactjs.org/docs/thinking-in-react.html


This content originally appeared on DEV Community and was authored by DEV Community


Print Share Comment Cite Upload Translate Updates
APA

DEV Community | Sciencx (2022-03-09T04:06:47+00:00) Understanding Inverse Data Flow. Retrieved from https://www.scien.cx/2022/03/09/understanding-inverse-data-flow/

MLA
" » Understanding Inverse Data Flow." DEV Community | Sciencx - Wednesday March 9, 2022, https://www.scien.cx/2022/03/09/understanding-inverse-data-flow/
HARVARD
DEV Community | Sciencx Wednesday March 9, 2022 » Understanding Inverse Data Flow., viewed ,<https://www.scien.cx/2022/03/09/understanding-inverse-data-flow/>
VANCOUVER
DEV Community | Sciencx - » Understanding Inverse Data Flow. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/03/09/understanding-inverse-data-flow/
CHICAGO
" » Understanding Inverse Data Flow." DEV Community | Sciencx - Accessed . https://www.scien.cx/2022/03/09/understanding-inverse-data-flow/
IEEE
" » Understanding Inverse Data Flow." DEV Community | Sciencx [Online]. Available: https://www.scien.cx/2022/03/09/understanding-inverse-data-flow/. [Accessed: ]
rf:citation
» Understanding Inverse Data Flow | DEV Community | Sciencx | https://www.scien.cx/2022/03/09/understanding-inverse-data-flow/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.