score:1
Accepted answer
return array of array problem
i think this function should little fix.
the function right now:
function atualizarcadastros(novocadastro){
setcadastros(cadastrosatuais => {
return [cadastrosatuais, novocadastro]
})
}
when we call atualizarcadastros({ c: 3 }), and we have cadastrosatuais = [{ a: 1 }]
we get new cadastros as [[{ a: 1 }], { c: 3 }]
.
but we want to get [{ a: 1 }, { c: 3 }]
so we can use spread syntax:
function atualizarcadastros(novocadastro) {
setcadastros(cadastrosatuais => {
return [...cadastrosatuais, novocadastro];
})
}
or better use concat:
function atualizarcadastros(novocadastro) {
setcadastros(cadastrosatuais => {
return cadastrosatuais.concat(novocadastro);
})
}
score:1
when you write like this
setcadastros({'id': id, 'nome': nome, 'cpf': cpf, 'rg': rg, 'data_nasc': nascimento, 'sexo': sexo})
atualizarcadastros(novocadastro)
novocadastro contains an old one as its value was read before setcadastros was called, so you can create an object and pass it to both: local state handler and to parent component function
const supernovocadastro = {'id': id, 'nome': nome, 'cpf': cpf, 'rg': rg, 'data_nasc': nascimento, 'sexo': sexo}
setcadastros(supernovocadastro)
atualizarcadastros(supernovocadastro)
Source: stackoverflow.com
Related Query
- How to update parent's state with a new object
- How can I update certain object in state with new data
- React hooks: How do I update state on a nested object with useState()?
- React js useState hook. How to update state of a json object with an a array in it when a checkbox is clicked
- How to update array within state object with react/redux
- ReactJS: How to access and update nested state object with dynamic key?
- How to update state (array of objects) with incoming object in Redux?
- How to update state of nested object by id with input select method?
- Update nested react state object with new arbitrary object
- How to update an existing array of objects (add a new object to the array) with the useState hook?
- How to update a react redux state with object spread properly?
- How can I update a state inside an array of object with Redux?
- how to update state with reducer, when my state is an array not an object
- State in Reactjs holding the previous state value, how to update the state with new edited form value
- how to update render with the new state
- How to update an object element in array with specific index, React State
- How to update a single object inside the array with useReduce on state change
- How to update value of a key in object in an array with the new value in React useState in TypeScript
- How can I add new a new object with key and values pairs to the existing state in class based component?
- How to add new Properties to File object in React with state value dynamically
- React + TypeScript - how to update state with single object with a "dynamic" set of changes
- React hooks: How do I update state with object
- How to update object in React state
- How to update state with usestate in an array of objects?
- Form created with React doesn't update the backing state object
- React. How to update the state with previous state?
- vue.js: how to update state with object?
- How to update multiple state properties with immer.js
- How to update state with setState before render
- Why does calling useState's setter with the same value subsequently trigger a component update even if the old state equals the new state?
More Query from same tag
- React native view layer
- React Router: Component not getting displayed
- Run lib callback function inside react component
- Update an object property in useState Hook,Property name should be get from a variable
- Nouislider slider is not working in react
- I am not able to to understand how to use useReferenceArrayInputContext in ReactAdmin
- Invalid prop `cookies` when running test with react cookies
- Running example with react-router in typescript 1.6 with TSX
- "React has detected change in order of hooks", but order seems preserved
- next/image loads very big images
- Typescript object possibly undefined when looking for objects inside an array
- Invalid hook call error when using React function component from npm package
- React Router <Component/> vs {Component}
- How to send values React Hook(props)
- How do i display formatted html code stored in javascript object
- How to Properly Configure Apache on a SPA React App that Resides on a Subdirectory that uses Nested URLs built from React Router?
- Prevent multiple on-click events from firing: ReactJS
- Removing an item causes React to remove the last DOM node instead of the one associated with that item
- Fit canvas to screen in React
- imgbb upload image in an album via api
- How to send data from NewsxWebPart.ts file to Newsx.tsx file
- React Quill Custom Ordered list
- Is it possible to implement open id connect SSO in ReactJS with IDP independent code?
- d3: drawing a multi-line line graph from a 2d array of objects
- How should flux handle routing?
- assign values to object via loop and then make it object again
- Mapping through array of objects after filtering and displaying dynamic common property as a header
- Asynchronous handling in useEffect
- Export / import a useState altering utility function?
- How to show and hide an element on button click in React?