I have learned about how to use Redux as the state management library, but most of my project is using react useContext + useReducer since it's not a huge project.
And as far as I know, the Redux is similar to react useReducer which is only managing the state. But I want to create a dedicated file where I can put all the functions as useContext does.
Is it possible to use useContext + Redux? or is there any better solution for it?
Thanks in advance, appreciate any kinda response.
Neither useContext nor Redux tell you anything about how to structure your files. Theoretically you could put everything into one file or split it up into hundreds.
There are recommended project structures though.
Take a look at the Style Guide, which gives an example of a recommended file/folder structure.
Also, stay away from most external tutorials that still show switch..case reducers, connect or folder structures with separate action, reducer and constant files. Those are heavily outdated. Follow the official tutorial instead:
https://redux.js.org/tutorials/essentials/part-1-overview-concepts
Related
I started learning React recently and managed to build a simple application. The app works well, but I ended up passing too many props to child components, which makes the code very messy. I was looking for ways to improve my code and found out that there are different ways to handle the state of my application. Most of my research pointed to either Redux or the useContext hook.
Besides making my code cleaner, does Redux or useContext help improve the performance of my application? Or are they actually worse than passing props when it comes to performance?
Thanks in advance!
Redux in itself isn't really slowing your App down if you do it right. What may cause your app to slow down is too many re-renders. Here is an article on how to prevent that : https://redux.js.org/faq/performance#wont-calling-all-my-reducers-for-each-action-be-slow
You can also read about performance in the offical Redux documentation: https://redux.js.org/faq/performance#wont-calling-all-my-reducers-for-each-action-be-slow
and this: https://redux.js.org/faq/general#:~:text=Redux%20is%20most%20useful%20when%20in%20cases%20when%3A&text=The%20app%20state%20is%20updated,is%20being%20updated%20over%20time
A slight downside to redux and useContext, is that you have to wait for the redux store to be available on the initial render, so you have to check if the state is available before using it. (state && console.log(state). However, this is still better than passing around a million props.
I'm an angular developer for many years and I tried to switch some of my new projects by using React & React Native, but I had an hard time deciding which state management could suit better for a project that could grow bigger, and my question was if I could use both solutions by having Redux as a central storage and the Context API for a better management to pass props to nested components, is it a good pattern to use both or should I stick to one of them.
It really depends on the use case.
If you're looking for top-down props pass down structure where the same data is used over multiple depths of components (such as Theme, Auth, etc.), Context API is the way.
If you're looking for a big store where you can dynamically manage data and constantly update it, Redux is the way.
If you need both, my opinion is that it's perfectly okay to do so.
They're not very heavy, (I guess a Redux.. a lil bit?) and they do have different purposes anyway.
I am doing a tutorial on React with Redux and Thunk and am having trouble understanding how some of the props are being sent to the different components. I think I understanding how the Flux Architecture in general is working, but the code itself filled with functions returning other functions and other syntax that is making my head spin. How the dispatcher is working for example is a complete mystery to me. Googling around I have read that this is a very "functional programming" approach to doing things in react. Can anyone point me to some resources to get up to speed with the type of Javascript used for Redux and Thunk?
Thanks!
Here are some links,
Redux.org
React-redux tutorial for beginners
Redux-thunk
I am new with react js.
I made two components to send/receive data using props between them.
But it gets more complex, I feel like I need to replace them with other solution like redux. I wonder there is the latest better solution to replace redux now for communication between two react components.
There was a lot of opinions between redux and context api like this post. But today, I can say yes, you can replace redux with just react itself. Because, currently react provides so much concepts just like the redux, context api. You can find react additional hooks like useReducer, useCallback, useMemo, etc.
But arguably, redux is still worth for maintaining project easily. You can have redux devtool, keep the code clean (state in store - a separate place for concerns).
There are more concerns over this. You may see this issue what future react will have. You may also find the medium blog helpful and the reddit discussion.
Worth Reading:
State management with react hooks and context api
Redux main task is to be a 'predictable state container', and, as you wrote, it also makes sharing data between components much easier. The main drawback is that it requires additional code, but I think that it is worth it.
For apps / websites that are not very small, I wouldn't use react / react native without it.
The way redux works may be a bit difficult to understand in the beginning. The following is a great article that helped me when I started using it: redux connect explained
I have been playing with ReactiveX for a bit and really like the approach. I have also been following Relay and Falcor for a while and understand the advantages of declarative data fetching.
I have seen people integrating react, redux with falcor, but have not seen any attempts to integrate React, Rxjs with either Relay or Falcor.
So I wanted to write a simple todo example, but before that wanted to ask whether it is a sound combination at all. Are there compatible and whether some advantages of one approach will negatively affect advantages of others.
Also if anyone can share some resources on this topic please do :)
There are several libraries that I found:
https://github.com/trxcllnt/reaxtor no React js though
https://github.com/FourSS/refar
UPDATE: there is an attempt by meteor developers to create their own version of relay using reactive graphql see Apollo Project They use observables for watch queries
Also there is a good library called relate which you can integrate with rxjs redux style state machine