Comments (1)
Hey @brumm
I'm glad you're interested! At the current state, ReSift is unopinionated when it comes to optimistic updates. We have a solution for when you want to keep state consistent in regards to a PUT vs a GET request but nothing currently for keeping a local expected mutation in sync. I'd be open to exploring adding features though.
However, with ReSift today, you can achieve the same result by using react state and a few effects:
Take a look at this example:
import React, { useEffect } from 'react';
import { useDispatch, useData, useStatus, isLoading } from 'resift';
// these are "fetches" which are tokens the represent your request
// see https://resift.org/docs/main-concepts/how-to-define-a-fetch
const getResource = /* ... */;
const updateResource = /* ... */;
function Resource() {
const dispatch = useDispatch();
// pull the latest synced resource from the server using `useData`
const resourceSyncedFromServer = useData(getResource);
// pull the status and create an `isSyncing` variable
const status = useStatus(getResource);
const isSyncing = isLoading(status);
// create some local state
const [resource, setResource] = useState(resourceSyncedFromServer);
// create an effect that will sync the server state to the local state
useEffect(() => {
if (isSyncing) return; // early return if something is still in the works
if (resourceSyncedFromServer) {
setResource(resourceSyncedFromServer);
}
}, [resourceSyncedFromServer, isSyncing]);
// create an effect that will sync the local state to the server state
useEffect(() => {
// prevent infinite loops
if (resource !== resourceSyncedFromServer) {
dispatch(updateResource(resource));
}
}, [resource]);
// this is an example of a handler.
// the idea is you can always just set the local state and it will automatically update on the server via the effects
const handleChange = e => {
const nextResource = getResourceFromEvent(e);
setResource(nextResource);
}
return (
<>{/* ... */}</>
);
}
It uses two effects to do the syncing of local state to server state. If you're interested in this pattern, I can try to flesh it out a bit more and we can have more formal discussions if we should move it internally to ReSift. Since ReSift is hooks based, it wouldn't be very difficult to create a custom hook that implements this pattern as well.
If you need the state to be more global, you can also write some custom hooks that use React context to provide state to any component in the tree without having to pass props.
Let me know what you think of the above approach!
However, if you always want to have this pattern of updating state locally, I would suggest checking out some other data fetching libs that revolve around this. Both zeit's SWR and tanner's react-query have built in "stale-while-revalidate" (SWR) approaches that might suit your needs better.
from resift.
Related Issues (20)
- Sharing fetches across different namespaces HOT 3
- Updating the http request function to have the ability to add custom headers per each fetch request. HOT 1
- Resift will not render on Android React Native projects with hermes enabled. HOT 1
- Set a default for the `key` property on the make function in `defineFetch` HOT 1
- Loader boundaries HOT 1
- Brainstorming APIs for Suspense HOT 1
- Proposal: Guard HOT 2
- useFetch does too much; add `useData` and `useStatus`
- Fetch naming conventions HOT 1
- Error handling improvements: add useError
- Checking and manipulating status
- Context Loader
- Suspense and Concurrent Mode in ReSift HOT 9
- Tutorial: ReSift Rentals
- Code examples page
- Bundle Size and Bundling HOT 2
- Roadmap/Backlog
- Allow more specification to the loading status behavior HOT 2
- Hoist merges to top-level HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from resift.