score:1
Accepted answer
If you need to persist any of the state to localStorage to allow reloading the app then it appears you should persist the targetKeys
state.
Example:
const App = () => {
// Lazy state initializer function to load from localStorage
// or fallback/default initial value
const [targetKeys, settargetKeys] = useState(() => {
return JSON.parse(localStorage.getItem("targetKeys")) || filteredGroups;
});
const [selectedKeys, setselectedKeys] = useState([]);
// persist state updates to localStorage
useEffect(() => {
localStorage.setItem("targetKeys", JSON.stringify(targetKeys));
}, [targetKeys]);
const handleChange = (nextTargetKeys) => {
settargetKeys(nextTargetKeys);
};
const handleSelectChange = (sourceSelectedKeys) => {
setselectedKeys([...sourceSelectedKeys]);
};
return (
<>
<Transfer
dataSource={DisplayItems}
titles={["Source", "Target"]}
targetKeys={targetKeys}
selectedKeys={selectedKeys}
onChange={handleChange}
onSelectChange={handleSelectChange}
render={(item) => item.title}
oneWay
style={{ marginBottom: 16 }}
/>
</>
);
};
When the component mounts you'll very likely want to refetch any data from your DBs to ensure you've the latest backend data, but if you needed any of this data at app start/mount then persist any other data/state your app/component needs.
Source: stackoverflow.com
Related Query
- JavaScript filter does not update the React local state
- React does not refresh after an update on the state (an array of objects)
- React Hooks: updating state using useState does not update the state immediately
- React component does not update with the change in redux state
- React cannot set state inside useEffect does not update state in the current cycle but updates state in the next cycle. What could be causing this?
- React context does not update state until the page is refreshed
- Why does the state object in react not update properly? Expanded and collapsed version of object differ
- React local state does not update after onclick
- React + Redux, component does not get update while the state is changing
- Why does calling react setState method not mutate the state immediately?
- React leaflet center attribute does not change when the center state changes
- React js changing state does not update component
- My React component does not update in the Safari browser
- npm run build does not update the react components
- Search filter does not work across the react-table on setState : React JS
- React hooks - setState does not update state properties
- useState react hook does not set the array state
- React useState() hook does not update state when child.shouldComponentUpdate() returns false
- React multiple callbacks not updating the local state
- ReactJS - SetState does not update the state when the state variable is a number
- reducer does not update state in react redux
- useState() in react does not update the data - is this the sort() issue?
- PrevState does not update state value immediately in React JS
- React router goes to proper URL but does not update the view
- wrote a React code snippet to implement conditional rendering and am not able to update the state of {Left} and {Right}
- In React useEffect does not update after the value has changed from other component
- why does it not update the sql row in the react web application?
- React Bootstrap Dropdown items does not update on state change
- setInterval in react does not update the setState
- Im trying to build a menu that has collapsible options, but the dom does not update even when state data updates
More Query from same tag
- Upload multiple images and save to array in React JS
- Material-ui List - Module not found: 'react'
- Can Someone help me to convert typescript code to React Code
- Structure Mobx project like Redux
- Getting 404 error while routing after sign-in in ReactJS
- I18N change language in Next.JS
- React warning uncontrolled component in child component
- Create variables without the createPages API in Gatsby?
- How can i fix "Uncaught TypeError: dispatch is not a function"?
- Handle infinite loop in componentDidUpdate
- Why does the new `Pick<T, K extends keyof T>` type allow subsets of `K` in React's `setState()`?
- How to substitute this.props.dispatch in a function?
- I have a button in my react app that i only want it to be pressed once
- Regex to match URL - number at end of string, but also match characters at start of string
- how to define type when the component is a union type
- How to setup Jest Watch Plugins with create-react-app
- How to use scrollIntoView in React?
- Textarea change cursor position when writing
- Text content doesn't wants to center itself inside <a> tag (React)
- Can I mapDispatchToProps without mapStateToProps in Redux?
- Adding React.Js to my Asp.net MVC core , is this possible/compatible?
- Creating a React app with 'create-react-app my-app' for the first time
- Tests react component (context.router - undefined)
- On a page refresh, my app gives me a TypeError: uid null when using firebase auth
- Printing out values from a subarray to a table
- Bootstrap Checkbox Not Toggling
- React setState function
- Display element for one person on hover
- react-pdf library is giving error at gatsby build time
- Why my green border doesn't toggle?(disappear)