score:3
Accepted answer
const [list, setList] = useState(data); // <-- (1) parent state passed as prop
// edits the data
const handleClick = (item) => {
setList(list => list.map(el => { // <-- (2) array.map shallow copy
if (el.payerName === item.payerName) {
return {
...el, // <-- (3) shallow copy array element
state: [...el.state, state.value], // <-- (4) shallow copy array
isActive: !el.isActive, // <-- new property
};
}
return el; // <-- not updating, just return current element
}));
};
score:0
const handleClick = (item) => {
let MyList = [...newList]
# this is brcause you are using map. So first push the data in MyList and then setState your newList, so that child component can re-render
MyList.forEach((el) => {
if (el.payerName === item.payerName) {
el.state.push(state.value);
if (!el.isActive) {
el.isActive = true;
}
}
});
# also include this
setNewList(MyList)
setUpdate([...update, item]);
}
score:0
const handleClick = (item) => {
const payer = list.find(el => el.payerName === item.payerName);
payer.state.push(state.value);
payer.isActive = true;
setUpdate([...update, item]);
}
score:0
function EditInsurance(props) {
const [parentData, setParentData] = useState([._cloneDeep(props.data))]);
...
}
score:0
//sets the data onLoad
useEffect(() => {
setList(JSON.parse(JSON.stringify(data)));
}, []);
score:0
const [data, setData] = useState([]);
const [editing, setEditing] = useState(null);
{!!editing && (
<EditInsurance state={editing} data={[...data]} setEditing={setEditing} />
)}
score:1
function EditInsurance(props) {
const [parentData, setParentData] = useState([...props.data]);
...
}
Source: stackoverflow.com
Related Query
- Changing the data in a child component without changing it in the parent
- Is there a way to force a re-render of a child component from the parent component without using changing its props in React?
- How to pass data from child component to parent component without the child component rerendering in React?
- Should the parent or child component fetch data in React?
- How do I get the Age data from child to parent component
- React router dom passing data from parent component to child router component does not pass the props.match
- How does the child component receive props from the parent component without listening for the lifecycle method?
- React collect child component data on some event from the parent component
- How to set a child component without rendering it in the parent component?
- React: swap a parent component without remounting the child component?
- How to handle the props from Parent Component to child component to populate props data in the form for updation
- Changing the state of parent component inside a child
- Can the child component receive old data for manipulation from parent component?
- React - Update data in the parent component after a change in child component
- Changing the state of a parent component using a function in the child compontent
- how to call a function from child components to parent component(function is in parent) without including the full component
- how to pass the data from child component to parent component
- React: How can you access the class name of a parent component from a child without passing it down as props?
- Child component is not getting data from the parent component
- React JS pass the data or child component to parent component
- Need to send the checkbox Filtered Data from Child Component to Parent Component : React Hooks, Redux
- How can I Send data to another Component without using Parent and Child Relation in React
- React: How to pass the data from function located on the child component into parent component?
- How to use the data from parent component to be the useState of child component?
- React hooks doing an infinite fetching when getting data from child but has no problem if the parent is a class component
- Get consolidated data from all the child components in the form of an object inside a parent component : React JS
- How do I use react context API to pass data from parent component to child component when using react-router in the parent component in React.js?
- Any better way to pass data from a child component to a function in a parent component without using bind()?
- I can not access the right data of property sent from parent to child component
- How do I pass my Form Data which is in the child component to the parent component(which also contains a form) and on submit log all the data?
More Query from same tag
- Getting a warning in console "You are calling ReactDOMClient.createRoot() on a container that has already been passed to createRoot() before."."
- webpack 4 react unexpected token ...(spread operator)
- Using .htaccess to forward all routes except one to home page for react-router v4
- Is there a good way to implement a div cross which expands onHover?
- On first launch choose language React Native
- passport.changePassword is not a function - passort-local-mongoose
- How do I pass props a component rendered with Enzyme.js?
- update root Component's property from the child Component with redux
- React JS: Get Context Data After getting success on API Call
- Read a file as base 64 to store it into a state variable
- Data pushed to array in existing object with axios
- Make React useEffect hook not run on initial render
- Types of property 'WebkitBoxOrient' are incompatible
- getting error while destructuring the data from react redux. why it is giving error and how to solve this?
- How to pass form input fields to another component in React?
- React Native: fetch request failed with error - TypeError: Network request failed(…)
- Using MetricsGraphics.js with ReactJS
- tailwindcss works with App.js but not with any of the components
- React js useEffect
- Uncaught TypeError: Cannot read property 'func' of undefined
- Is there a way to pass an Apollo query selection through react props?
- How to use axios / AJAX with redux-thunk
- PropType for Object Where Keys are IDs
- Javascript, then method not waiting for async function in React
- rails ajax post invalid authenticity token
- React binding method in constructor - how to pass in Argument
- useEffect doesn't fire when data change
- Keeping state of input breaks app when value is undefined
- how to handle local video files Reactjs
- How to define constants in ReactJS