score:1
unlike setting the state in a class component, usestate
doesn't merge the object you pass, and you have to do it manually when setting the state. when you set age
, for example, you replace the entire state object, which makes name
to be undefined
, and vice versa.
use functional update, and create a new state based on the previous state object before setting it.
const { usestate } = react
const adduser = () => {
const initialuserstate = {
id: null,
name: '',
age: 0
}
const [users, setusers] = usestate(initialuserstate)
const handlechange = (e) => {
// create the new state and set it
setusers(prev => ({ ...prev, [e.target.name]: e.target.value }))
e.preventdefault()
}
return (
<div>
<input name="name" type="text" value={users.name} onchange={handlechange}/>
<input name="age" type="number" value={users.age} onchange={handlechange}/>
</div>
)
}
reactdom.render(
<adduser />,
root
)
<script crossorigin src="https://unpkg.com/react@17/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@17/umd/react-dom.development.js"></script>
<div id="root"></div>
score:3
this page and this paragraph can help you understand the problem.
when you get the updated input through the onchange
event listener you don't need to pass again the data through the value
attribute.
when data is passed through the value
attribute the component is considered "controlled". this means that the component is controlled by your code and shouldn't receive user input.
if you just want to set a default value you can use the defaultvalue
attribute.
to remove the warning just remove the value={/* something */}
.
Source: stackoverflow.com
Related Query
- React a component is changing an uncontrolled input of type checkbox to be controlled
- A component is changing an uncontrolled input of type checkbox to be controlled in React JS
- Warning: A component is changing a controlled input to be uncontrolled in React js
- React input, I defined the state into initial state but still get warning changing uncontrolled to controlled input
- Warning: A component is changing a controlled input to be uncontrolled input in react js
- React Formik Warning: A component is changing an uncontrolled input to be controlled
- React Input Warning: A component is changing a controlled input of type text to be uncontrolled
- Email Input Warning - A component is changing a controlled input of type text to be uncontrolled
- A component is changing an uncontrolled input of type text to be controlled error in ReactJS
- Material UI Select Component- A component is changing a controlled input of type text to be uncontrolled
- React form error changing a controlled input of type text to be uncontrolled
- formik warning, A component is changing an uncontrolled input of type text to be controlled
- ReactJS - Warning: A component is changing an uncontrolled input of type text to be controlled
- Uncontrolled Input to Controlled Input Warning in React Hook Form and Material UI's TextField
- A component is changing an uncontrolled input of type email to be controlled. Input elements should not switch from uncontrolled to controlled
- React: Warning, a component is changing an uncontrolled input to be controlled
- Getting "A component is changing an uncontrolled input to be controlled (...)" when using KeyboardDatePicker
- React component is changing an uncontrolled input of type checkbox
- A component is changing a controlled input of type text to be uncontrolled. Input elements should not switch from controlled to uncontrolled
- Warning Controlled Component Changing to Uncontrolled
- React.JS Typescript - OnChange says "A component is changing a controlled input of type text to be uncontrolled in OnChange" for State Object
- Can you help me to get rid of this? Warning: A component is changing an uncontrolled input of type search to be controlled
- A component is changing a controlled input of type text to be uncontrolled (useEffect)
- Warning when changing controlled input value in React
- Textarea issue in reactJS : A component is changing a controlled input of type undefined to be uncontrolled
- Warning: A component is changing an uncontrolled input to be controlled
- How Can I Avoid This Warning ' A component is changing an uncontrolled input to be controlled. This is likely caused by the value changing from...'?
- Warning: A component is changing an uncontrolled input to be controlled - which component are they referring to?
- Warning: A component is changing a controlled input of type undefined to be uncontrolled
- ReactJS - Warning: A component is changing an uncontrolled input of type text to be controlled - edit function
More Query from same tag
- how can I access specific index of array in react js [0]
- Do I need to install @types/react to work with React in a TypeScript project?
- React how to avoid css overide other components?
- Passing JSX to components vs dangerouslySetInnerHTML
- event bus in React?
- Material-UI show letter avatar when ther is no image
- Simple explanation as to why this code wouldn't work
- How do I get my react js app to start watching my sass files?
- react-bootstrap Accordion not collapsing with map
- React list, show active item onClick. Without re-rendering the list
- Adding modal popup to cells in ant design table
- What's the fastest way to mutate a single object property inside a large array of objects?
- How and where to pass JSON data as a prop when creating components
- Reset input on uncheck checkbox (ReactJS + Formik)
- React. Securing communication between frontend and backend located on the same server
- SemanticUI menu - first item always active
- React - loading images from a remote server (absolute path require/import)?
- react js eventListeners not removed as expected
- Styling the UI Fabric Date Picker Callout
- Which is a better to write a react component with styled-components?
- Inserting component in <Head> tag in Next.js
- React: Get target's DOM children
- How to change the hover effect for react-bootstrap navbar link?
- react download the document in the same page
- How to share Session storage data with other opened browser tabs in React
- CanJS - Incorrect list objects type after mapping
- Navigation Drawer in React-Native
- jest test:coverage with react propTypes / defaultProps
- Link button to heading in same page
- How to pass data between sibling components React?