score:13
In React, you should avoid doing state-mutations, which means do not explicitly change something belonging to the existing state. In order for React to decide whether or not to re-render to reflect your changes, it needs to register a new-state.
Get in a good habit of creating a clone or deep-copy of the state, and then updating that.
Try something like this instead. In the below, we use the spread operator {...}
to clone the state before updating it:
const nullableEntry = {
ID: "",
day: "",
hours: 0.0,
note: ""
};
const MonthTable = props => {
const [editing, setEditing] = useState(nullableEntry);
function handleHoursInput(e) {
let newEdit = { ...editing };
newEdit.hours = e.target.value;
setEditing(newEdit);
}
return (
<input
type="number"
value={editing.hours}
step="0.01"
onChange={handleHoursInput}
className="form-control"
name=""
/>
);
};
Working sandbox: https://codesandbox.io/s/hopeful-bogdan-lzl76
score:0
you can update state in functional components in this way
const [show, setShow] = useState(false)
const [scopesData, setScopesData] = useState(scopes)
const submitCallBack = (data) => {
setShowhowAlert(true)
setScopesData(...scopes, scopes.push({
id: generateKeyHash(),
title: data.data.scopetitle,
}))
}
these 3 lines of codes update the state correctly
[setScopesData(...scopes, scopes.push({
id: generateKeyHash(),
title: data.data.scopetitle,
}))
score:5
Do not mutate state, editing.hours = e.target.value
mutates original state
change your handleHoursInput
function to something like this
function handleHoursInput(e) {
let hours = e.target.value;
setEditing({...editing, hours});
}
Source: stackoverflow.com
Related Query
- react is not updating functional component state on input change
- React Child Component Not Updating After Parent State Change
- Functional Component in React is not updating on props change
- React Component not updating with state change
- functional component is not re-rendering after state change in React
- react state not updating on input change
- Checkbox state not updating in react Hook functional component
- React JS component not updating on state change
- Component not updating on state change with React Router
- React Redux functional component updating state not working
- React functional component not re-rendering with state change
- React functional component setState not updating the state
- React functional component state change does not trigger child component to re-read its props
- React functional component is not re-rendering on state change
- React .map not re-rendering on state change in functional component
- React functional component not re-rendering on its own state change
- React component not re-rendering on state change
- React component not updating when store state has changed
- React shouldComponentUpdate() is called even when props or state for that component did not change
- Why is my react component not updating with state updates?
- React Mobx - component not updating after store change
- Props not updating when redux state change in React Hooks
- Component not updating when I change the props that I pass to it in React
- React router: component not updating on url search param change
- ReactJS component textarea not updating on state change
- react component state change after ajax call but does not rerender component
- React Child with useEffect() not updating on Parent State Change
- Component not updating on Map (dictionary) state change using hooks
- React child component not re-rendering on parent state change
- React component not updating on final dispatch and update of redux state
More Query from same tag
- React route not matching query string
- Shallow Rendering Jest Snapshots
- Rendering previous state instead of current value selected in dropdown
- Accessing Data in Object over http put request
- React and data binding
- How to mock third-party library custom events with jest and react testing library
- Variable of state update on LayersControl.Overlay checkbox toggle
- ScrollMagic loses functionality after Gatsby build
- Jest can't perform a React state update on an unmounted component + react state updates should be wrapped into act
- How to get default values from Typescript object?
- Highchart- OnClick event, get "category" and "name" from stacked bar chart
- "Document is not defined" Webpack + React on Rails + Server Rendering
- React Bootstrap 5 - Tabs component with Previous Next Button
- Typing switch statement
- Type of property value in generic object
- Check all nested boxes when parent is selected
- How do I interpret the following error I've received?
- Redux/React: Cannot read property 'type' of undefined when changing activePage
- Why do I need to hit enter in Web address bar with React Routes?
- Antd Calendar is not displaying the proper outline
- How to pass props of a nested object to a component?
- split style[hash].css into dynamically loaded chunks
- Im having troubles importing an img using a local data.json file which has a path object (REACT)
- Dispatching function does not "always" update the props
- Sort of array of objects not working
- input is not updated when button is clicked
- React map through two arrays without duplication
- ClassName styles not working in react
- ReactJS App on PHP backend - how to hot reload on local machine?
- How to test a range of numbers with Jest?