Revisiting Hooks implementation
See original GitHub issueYou want to:
Discuss required changes to current ‘hook-ish’ implementation for Hooks support.
Details:
As you can see here, we’ve got a nice wrapper on top AsyncStorage
, to mimic hooks usage.
This discussion here is to come up with better and valid implementation, that leverages hooks API.
Example implementation could look like this:
function useAsyncStorage(key, defaultValue) {
const [storageValue, updateStorageValue] = useState(defaultValue);
useEffect(() => {
getStorageValue();
}, []);
async function getStorageValue() {
let value = defaultValue;
try {
value = (await AsyncStorage.getItem(key)) || defaultValue;
} catch (e) {
// handle here
} finally {
updateStorageValue(value);
}
}
async function updateStorage(newValue) {
try {
await AsyncStorage.setItem(key, newValue);
} catch (e) {
// handle here
} finally {
getStorageValue();
}
}
return [storageValue, updateStorage];
}
The problem in here is that, once we got a value inside AsyncStorage, we’ll be getting two rerenders one component mounting - one returning the default value, second the actual stored data.
I’m more than happy to discuss this, so please let me know what you think.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:4
- Comments:18 (3 by maintainers)
Top Results From Across the Web
Revisiting React Hooks. Originally published at… - Medium
In this example we will take a closer look at the two of the most important built-in hooks — useState and useEffect.
Read more >Deep dive: How do React hooks really work? - Netlify
In this article, we reintroduce closures by building a tiny clone of React Hooks. This will serve two purposes - to demonstrate the ......
Read more >React Hooks Revisited: Introduction - DEV Community
Introduced in React v16.8, the hooks API represents a change in how developers compose their components. Intended to compartmentalize blocks ...
Read more >Understanding common frustrations with React Hooks
What problems do React Hooks solve? · What was wrong with class components? · Yet another JavaScript paradigm to learn · To hell...
Read more >Revisiting Hooks — sourcehut lists
Revisiting Hooks == Context After implementing the initial work for storage hooks based on RFC 703[0], the next task was to wire up...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
We’re going to revisit it in v2.