This is called “lifting state up”. We will remove the local state from the TemperatureInput and move it into the Calculator instead. If the Calculator owns the shared state, it becomes the “source of truth” for the current temperature in both inputs. Instructions for bypassing your router on your home network. Study abroad begins with language courses abroad and university entrance preparation. About Irina Afonskaya. I am Russian painter, photographer and poetess now living in Bulgaria in beautiful resort Saints Constantine and Helen. I have another profession having diploma of philologist in Russian literature of Moscow State University, but also studied painting in People's Art University in Moscow. Choose Shape Get Boosters! Give this link to your friends and play together: Copy ↻ Controls.
- For example,
- Можно ли вернуть залоговую бу машину другому владельцу
- Параболическая антенна mimo своими руками
- Our Gallery
- Your bosters:
- Войти на сайт
- Как правильно перечислять ндфл с отпускных 2019
- Lifting State Up
Often, several components need to reflect the same changing data. We recommend lifting the shared state up to their closest common ancestor. In this section, we will create a temperature calculator that calculates whether the water would boil at a given temperature.
We will start with a component called BoilingVerdict. It accepts the celsius temperature as a prop, and prints whether it is enough to boil the water:. Next, we will create a component called Calculator. Additionally, it renders the BoilingVerdict for the current input value. Try it on CodePen. Our new requirement is that, in addition to a Celsius input, we provide a Fahrenheit input, and they are kept in sync. We can start by extracting a TemperatureInput component from Calculator.
We will add a new scale prop to it that can either be "c" or "f" :. We can now change the Calculator to render two separate temperature inputs:.
Можно ли вернуть залоговую бу машину другому владельцу
This contradicts our requirement: we want to keep them in sync. These two functions convert numbers. We will write another function that takes a string temperature and a converter function as arguments and returns a string.
We will use it to calculate the value of one input based on the other input. It returns an empty string on an invalid temperature , and it keeps the output rounded to the third decimal place:.
Currently, both TemperatureInput components independently keep their values in the local state:. However, we want these two inputs to be in sync with each other.
When we update the Celsius input, the Fahrenheit input should reflect the converted temperature, and vice versa. In React, sharing state is accomplished by moving it up to the closest common ancestor of the components that need it. We will remove the local state from the TemperatureInput and move it into the Calculator instead.
It can instruct them both to have values that are consistent with each other.
Параболическая антенна mimo своими руками
Since the props of both TemperatureInput components are coming from the same parent Calculator component, the two inputs will always be in sync. First, we will replace this.
We know that props are read-only. When the temperature was in the local state, the TemperatureInput could just call this. However, now that the temperature is coming from the parent as a prop, the TemperatureInput has no control over it.
Now, when the TemperatureInput wants to update its temperature, it calls this.
There is no special meaning to either temperature or onTemperatureChange prop names in custom components. We could have called them anything else, like name them value and onChange which is a common convention.
The onTemperatureChange prop will be provided together with the temperature prop by the parent Calculator component.
It will handle the change by modifying its own local state, thus re-rendering both inputs with the new values. We will look at the new Calculator implementation very soon. We have removed the local state from it, and instead of reading this. Instead of calling this.
It is the minimal representation of all the data we need to know in order to render both inputs. For example, if we enter 37 into the Celsius input, the state of the Calculator component will be:.
If we later edit the Fahrenheit field to be , the state of the Calculator will be:. We could have stored the value of both inputs but it turns out to be unnecessary. It is enough to store the value of the most recently changed input, and the scale that it represents. We can then infer the value of the other input based on the current temperature and scale alone.
Войти на сайт
Now, no matter which input you edit, this. One of the inputs gets the value as is, so any user input is preserved, and the other input value is always recalculated based on it.
Usually, the state is first added to the component that needs it for rendering. Then, if other components also need it, you can lift it up to their closest common ancestor.
Instead of trying to sync the state between different components, you should rely on the top-down data flow. Additionally, you can implement any custom logic to reject or transform user input. For example, instead of storing both celsiusValue and fahrenheitValue , we store just the last edited temperature and its scale. The value of the other input can always be calculated from them in the render method.
Как правильно перечислять ндфл с отпускных 2019
This lets us clear or apply rounding to the other field without losing any precision in the user input. When you see something wrong in the UI, you can use React Developer Tools to inspect the props and move up the tree until you find the component responsible for updating the state.
This lets you trace the bugs to their source:. Edit this page.
Lifting State Up
Main Concepts. Advanced Guides. API Reference.
Hooks New. Previous article. Composition vs Inheritance.