Logo

dev-resources.site

for different kinds of informations.

React - How to load a form with the latest data

Published at
12/31/2022
Categories
react
reacthooks
useref
typescript
Author
thiagocbarreto
Author
14 person written this
thiagocbarreto
open
React - How to load a form with the latest data

Recently I had the following situation I needed to solve:

On a page with two tabs, the first one being a form, make sure the latest form values inserted by the user show up when they come back from the second tab.

Here is the component tree:
Tree diagram with "Page" as parent and "TabBar", "FormTab" and "InfoTab" as direct children

Here is the base code (see how the form state is lost on tab change):

Solution #1: Just lift the state up

  • move useState to parent component

Here the parent state is updated on every input change.

Solution #2: Pass state to parent on unmount, load it on mount

  • Parent: add useState
  • child: keep useState + add useRef + add useEffect (useRef is needed for us to be able to get the latest state on unmount without needing to listen to its every change).

Here the parent's state is updated only once during the life of the FormTab component, when the user leaves the form tab.

Useful when you have multiple inputs/computations going on so updating the parent's state on every input change costs you a lot.


Quick tip: for professional work, try using react-hook-form. It's a great library for form-related work (validation, submit, error messages, etc.).

useref Article's
29 articles in total
Favicon
How to create components interfaces using useRef/forwardRef/useImperativeHandle
Favicon
Hooks Behind The Scenes 3, UseRef!!!
Favicon
Mastering React's useRef Hook: Working with DOM and Mutable Values
Favicon
Understanding useRef: A Beginners Guide
Favicon
useRef Hook Explained
Favicon
useRef() in React . . .
Favicon
React State Management: When & Where add your states?
Favicon
Unlocking the Power of useRef: Besic to Advanced Examples for React Developers
Favicon
Leveraging useRef in TypeScript for Efficient DOM Manipulation
Favicon
Unlocking the Power of React Hooks
Favicon
React.useRefの理解を含めるエクササイズ
Favicon
Mastering React's useRef Hook: A Deep Dive
Favicon
Unlocking the Power of useRef in React: 12 Essential Use Cases
Favicon
Série React Hooks: useRef
Favicon
Toggle Password Visibility Using React useRef
Favicon
Stop using useState for everything
Favicon
Using React hooks to develop a Video Player
Favicon
React - How to load a form with the latest data
Favicon
React: сфокусировать поле ввода по чекбоксу
Favicon
How do I check/uncheck all checkboxes with a button In React Js using useRef() Hook ?
Favicon
Multiple item using one ref
Favicon
Closures and useEffects
Favicon
I need help. TypeError: Cannot read properties of undefined (reading 'current')
Favicon
Using React useRef Hook to access immediate past props or state.
Favicon
Compare Props in React Functional Components.
Favicon
Creating infinitely scrolling SPA using React
Favicon
useRef()가 순수 자바스크립트 객체라는 의미를 곱씹어보기
Favicon
Complete useRef() hook with live code examples
Favicon
Useful Patterns with React hooks

Featured ones: