Comments (5)
The HTML spec says the value has to be a string (see here), and it looks like React strongly wants all values to be strings to comply with this spec.
There's a check to make sure the value attribute is a string.
Values get coerced to strings here.
@types/react
says the type of value is:
value?: string | ReadonlyArray<string> | number | undefined
from react.
The HTML spec says the value has to be a string (see here), and it looks like React strongly wants all values to be strings to comply with this spec.
I thought it would be better if react supported doing so, could avoid some issues. Is there any reason why React wants all values to be string?
from react.
import "./styles.css";
import React,{useState} from "react";
export default function App() {
const [a, setA] = useState();
return (
<input
type="date"
value={a}
onChange={(e) => {
setA(e.target.value);
}}
/>
);
}
from react.
You are correct that passing a Date object directly to the value prop of a date picker component in React can be problematic. This is because the value prop expects a string in a specific format, and passing a Date object directly will result in an incorrect format that the browser cannot understand.
To avoid this issue, you can use a library like moment.js or date-fns to format the Date object into a string that can be understood by the date picker component. These libraries provide functions to format dates in a variety of formats, including the format required by the date picker.
Alternatively, if you don't want to use a library, you can manually format the Date object using the built-in Date methods. For example, you could use the getFullYear(), getMonth(), and getDate() methods to get the year, month, and day of the Date object, and then concatenate them into a string in the format required by the date picker.
It would be nice if React had built-in support for passing Date objects directly to the value prop of a date picker component, but at the moment, manual conversion is still required to ensure that the date is displayed correctly.
from react.
It would be nice if React had built-in support for passing Date objects directly to the value prop of a date picker component
That's why I opened this issue
from react.
Related Issues (20)
- [DevTools Bug] Cannot remove node "226752" because no matching node was found in the Store. HOT 11
- [DevTools Bug]: Regression - profiling doesn't store props value HOT 1
- [DevTools Bug]: Using different React instances across multiple frames throws errors HOT 4
- Bug:
- Props passed to components in an array are never updated HOT 6
- Bug: Don't crash the app if an async component is accidentally used on the client HOT 1
- Bug:
- Bug: Unable to type new character when textarea is focused, happen when using Japanese IME on IOS HOT 4
- Built-in Form Handling HOT 2
- Bug: Auto generated preload links should respect fetchpriority if specified HOT 5
- Bug: useTransition and uSES do not work together, uT is not resilient to amount-of-render HOT 1
- Bug: Dialog issue - Multiple modals appearing and close button not working HOT 1
- Bug: React 18 upgrade getting uncaught runtime errors HOT 2
- [DevTools Bug]: Strict mode badge points to the old docs HOT 4
- Update copyright text to Meta instead of Facebook HOT 5
- Bug: Cannot read properties of null (reading 'useState') HOT 1
- Bug: In a recursive components, `_debugSource` doesn't get updated HOT 3
- Bug: Uncaught (in promise) DOMException: Failed to execute 'postMessage' on 'Window': function () { [native code] } could not be cloned. HOT 2
- Bug: useId IDs have special characters that conflict with certain DSLs (like CSS) HOT 2
- [question] Is there any reason not to set this field to read-only?
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.