Just the code?
There are many options to handle state in react applications. Obviously you can use setState for some small logic, but what if you ...
For further actions, you may consider blocking this person and/or reporting abuse
Finally! A Context/Reducer tutorial which uses typescript and DOESNT just show how to change the theme name... Any chance you would be willing to add a custom Dispatcher which could resolve promises from REST API Calls? All of our API calls return Promises, and the dispatch method in React cannot resolve Promises, so I would love to see an example of a "Dispatcher" which wraps the default dispatch function to handle resolving Promises!
It's not a best practice to include your API calls in your reducer. The purpose of your reducer is to accept incoming state changes and apply them to the context.
Instead, when an action occurs in a .tsx component, write code like this:
apiCallFunction(args).then(result => dispatch(Actions.ThisAction, result))
This will cause the context to be updated when the api call finishes, and then your reducer can apply the results, update the context, and React will handle updating the DOM.
Greate post! although I have a question the way you have the type right now with the actions being a Discriminated unions of all the actions that created by ActionMap of all the reducers. This is very hard to manage when you have alot of reducers. There is no separation of concern. How would I go about moving each reducer into its own files. Then would I import each and every actions from all the reducer so I can use discriminated unions? I think there should be a better way, but I'm not knowledgable to do that yet. Do you have any suggestion?
Thanks! Yes I know if you have more than three reducers the types for the main reducer can increase, and be hard to manage.
I don't know how to improve the
action
type, thisaction
has to be a union ofproduct
andshoppingCart
actions because when usingdispatch
you can use either of both actions. To know exactly what action I need to use, maybe you could try with generics and conditional types. I think you could pass a generic type through Context to enable just certain types foractions
andstate
. It's an idea, maybe works. I'll try to implement it.So I found this on a stack overflow post
all you have to do is pass an object with key and value is the reducer. I'm searching about generic to convert this function to be type-safe. But if you have any idea please share. Thank you!
As it turned out your way of doing the combine reducers also works for type-safe all you gotta do is use type assertion ie:
By using the 'as' keyword there you can now get rid of the unions type on the action arg of your reducers
Hey, you are right, type assertion works well in this case, it makes the code look cleaner without the union types.
Thanks a lot.
Finally, I've found the exact thing that I was looking for. However, I still have a question. Can you show me how to initialize state from child nodes using this approach. What I wanted to achieve is something that is similar on this:
const [state, dispatch] = useReducer(
SampleReducer,
initialState,
() => {
return { field1: sampleValue1, field2: sampleValue2}
}
)
Thank you in advance.
Hey, I'm glad this helps you, according to react docs you can initialize a state passing a third argument. reactjs.org/docs/hooks-reference.h....
Yup, it same as what I also mentioned above. But, how would you achieve it in a context since the reducer is already part of the context?
oh. maybe you can pass the function through the context to the reducer? I'm going to try it and if I'm able to do it, I'll share the code :)
Yup, looking forward to it. That is what I wanted to achieve, I saw also in hooks document that we can create an init function that can be call through dispatch, however, not yet tried it.
Hi do you know how do I apply middleware with this config? I have been looking into this and i only got as far as create a single middleware and apply it but can't find how to apply multiple middleware
I haven't tried to apply multiple middlewares, let me try, and if I found a way I'll let you know.
Amazing tutorial that helper in my current project!
I have one question: Is it possible to add new type in the same reducer ->Types.Get? It would be returning one product by incoming id. The input would be like in Types.Delete action.payload.id. It could be required to extend reducers operation in order to show the user one product.
Probably change of state would be required but I may be wrong:
const initialState = {
products: [],
selectedProduct: -1,
shoppingCart: 0,
}
Very good article, help me a lot. Just to add some salt, I do prefer to manage my reducers indexing an object instead of the traditional function with switch. Example below:
The missing types are already exposed in the article.
Great Article, thank you for that - I Was struggling with correct typings for store a little bit too long :P
Your article made it clearer to me a few things I was unsure about. Thank you @elisealcala , well done!
Great post !
Excellent post, very well written!! It fits perfectly to what I'm doing here!! :claps
This is excellent. I love that it is fully typed.
Could anyone tell me why type ActionMap need to extends { [index: string]: any }? Thx so much.
how the create and delete works?
your code is not complete.
Hi, I update the code here. codesandbox.io/s/context-reducer-t..., check the
List
component. Basically, I create a state to handle the form and then just list the products.Thank you this was very helpful - I appreciate the example!!
(you have a typo on the last slide
useContex --> useContext
Thank You for this,context+reducer works beautifully in my code now
Great article! It would be nice to have an article on how to test it
Great article! It would be nicz to have an article on how to test it
Great post. Thanks for this