Refs in React JS | #ReactJS | EP -13
Refs provide a way to access DOM nodes or React elements created in the render method.
In the typical React dataflow, props are the only way that parent components interact with their children. To modify a child, you re-render it with new props. However, there are a few cases where you need to imperatively modify a child outside of the typical dataflow. The child to be modified could be an instance of a React component, or it could be a DOM element. For both of these cases, React provides an escape hatch.
When to Use Refs
There are a few good use cases for refs:
Managing focus, text selection, or media playback.
Triggering imperative animations.
Integrating with third-party DOM libraries.
Avoid using refs for anything that can be done declaratively.
Don’t Overuse Refs
Your first inclination may be to use refs to “make things happen” in your app. If this is the case, take a moment and think more critically about where state should be owned in the component hierarchy. Often, it becomes clear that the proper place to “own” that state is at a higher level in the hierarchy.
Creating Refs
Refs are created using React.createRef() and attached to React elements via the ref attribute. Refs are commonly assigned to an instance property when a component is constructed so they can be referenced throughout the component
#reactjs #reactjstutorial #reactjsbeginners
#jasacadamy
Видео Refs in React JS | #ReactJS | EP -13 автора Реактивные Навыки для JuniorDev
Видео Refs in React JS | #ReactJS | EP -13 автора Реактивные Навыки для JuniorDev
Информация
28 ноября 2023 г. 14:07:23
00:05:43
Похожие видео