Comments (5)
The browser treats the value
property different for inputs of type hidden
vs others. If you set the value of a type=hidden
input, it will not just update the value property but update the DOM attribute and therefore change the defaultValue
. On re-render, React detects that the defaultValue
is not up to date and changes it back to the value you provided as defaultValue
. For other inputs, the browser does not update the DOM attribute, so defaultValue
will not change.
As a workaround you can change the defaultValue
:
defaultValue={hiddenFields.current["HIDDEN_TYPE_FIELD"]?.value ?? "Bob Ross"}
from react.
I also tried with the latest React 18.3.0, isn't fixed:
https://codesandbox.io/s/agitated-tharp-gxr7z2
from react.
Though a hidden input's value can't be changed by the end user, it should however be able to be changed by the app:
The element's value attribute holds a string that contains the hidden data you want to include when the form is submitted to the server. This specifically can't be edited or seen by the user via the user interface, although you could edit the value via browser developer tools.
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/hidden
And in fact it does still work in a React app, as long as the dom view isn't updated, see this example:
https://codesandbox.io/s/dreamy-chaum-ct8czg
Only if the dom view is updated, the value is not persisted.
from react.
Hello, @JohnRDOrazio the ref local variable persists the data does not see on UI after re-rendering the components via the state variable.
In the code of code sandbox hidden inputs don't display their values directly on the page, but their values can be accessed programmatically. In this case, the updated values are correctly stored in the hiddenFields ref object, and when we click "Finalize," the finalizeForm function retrieves these values and updates the results state.
from react.
@MeenuyD I don't believe you have looked closely at the codesandbox examples I provided. The value of the hidden inputs are NOT persisted in the ref
variable, if the dom view is refreshed.
from react.
Related Issues (20)
- Bug: ref trigger useEffect execute HOT 10
- Bug: Internal React error: Expected static flag was missing. Please notify the React team HOT 2
- Bug: useContext
- Bug: uSES with synchronous updating a component causes a warning HOT 6
- Bug: StrictMode not work for `reactive` data trigger a update
- Bug: useState cut out to Component in iOS behaves strangely.
- Bug: form data is lost upon hydration HOT 3
- Bug: Uncaught Error: ReactDOM was loaded before React. Make sure you load the React package before loading ReactDOM. HOT 1
- Bug: All child components will rerender if providers not rendering children from props HOT 2
- Bug: react-dom.development.js:16572 Uncaught Error: Should have a queue. This is likely a bug in React. Please file an issue.
- Bug: [Firefox] Removing an element with contentEditable="true" within an iframe and an RTL selection causes an exception
- [eslint-plugin-react-hooks] (react-hooks/exhaustive-deps) allow configuring a prop to throw errors with unnecessary dependencies in useEffects HOT 7
- Let's See This Project about GodJobForReact-Vercel HOT 3
- Bug: HOT 5
- Provide a method to create synthetic events HOT 2
- Bug: Resetting the value comes from dynamic `defaultValue` of `<input type="search" />` won't trigger `onChange` HOT 9
- React - npm audit - 4 moderate severity vulnerabilities - Need solution please. HOT 2
- Bug: react-devtools-inline crashes when Function.prototype.toString is not configurable. HOT 1
- Bug:in.tsx:6 Warning: ReactDOM.render is no longer supported in React 18. Use createRoot instead. Until you switch to the new API, your app will behave as if it's running React 17. Learn more: https://reactjs.org/link/switch-to-createroot printWarning @ react-dom.development.js:86 error @ react-dom.development.js:60 render @ react-dom.development.js:29670 (anonymous) @ main.tsx:6 HOT 2
- Bug: Clicking file input label that's rendered in the list changes onChange handler behavior(to non-react behavior) HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react.