react-hooks-async | React custom hooks for async functions | Frontend Utils library
kandi X-RAY | react-hooks-async Summary
kandi X-RAY | react-hooks-async Summary
JavaScript promises are not abortable/cancelable. However, DOM provides AbortController which can be used for aborting promises in general. This is a library to provide an easy way to handle abortable async functions with React Hooks API. It comes with a collection of custom hooks that can be used as is. More custom hooks can be developed based on core hooks.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
- Starts the application .
- returns an array of indexes to the array
- Takes an array and returns it as array .
- Convert iterable to an array
- Watches indices to a non - iterable array
- Represents a non - iterable array .
- Turns array into an array .
- private helper methods
- Helper function for creating iterable loops
- Convert array - like to an array .
react-hooks-async Key Features
react-hooks-async Examples and Code Snippets
Community Discussions
Trending Discussions on react-hooks-async
QUESTION
I want to use nested reducers instead of having nested switch statements (can you even do that?) in the main reducer that's passed as the first argument to useReducer
. This is because my reducer function depends on more than one switch
(first operation, then fruit type).
I've looked up "nested reducers" but solutions to those questions seem to all be tied to redux and combineReducers
, of which there is no equivalent with Hooks.
Demo code (even though codesandbox is down again):
It doesn't actually show in the codesandbox (because the sandbox itself isn't working properly) but on my own machine I get Uncaught TypeError: fruits.apples.map is not a function
after I click the Add button. However, before that, map
works fine and all items are rendered as expected.
ANSWER
Answered 2020-Mar-16 at 14:16There were some slips in your code, like class
instead of className
, missing key
attributes. I modified your sample, have a look here.
Its also important that reducers are pure functions - always return a new state when a change is triggered by a suitable action, don't mutate the previous state (and nested properties). If no the reducer can't handle the action, just return the previous state - don't throw in the reducer, that would also make it impure.
An alternative to your shape would be to make each child reducer responsible for some sub state of the whole state tree in order to make it more scalable and composeable (Link). So one reducer for apples, one for bananas and one oranges (first fruit type, then operation). But in principle, you can handle the shape like you want/need it.
Hope, that helps out.
Update:If you seek an Redux-like implementation of combineReducers
for useReducer
, also have a look at Lauri's answer, though I recommend to use a different implementation for most cases. In the following sample each reducer only gets its own part ("slice") of the state, which reduces its complexity. You also can scale this solution pretty well - just add a new property + reducer:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install react-hooks-async
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page