Why use redux-saga? [closed] - reactjs

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I'm recently getting into React.js and learning with a small project.
While studying, I couldn't figure out clearly why I used react-saga or react-thunk.
It is said to process redux state value asynchronously.
But don't we already have async / await?
Shouldn't we call api from a component file using async / await to get a value from the server and store it in the redux store?
I've seen a lot of articles on why to use react-saga, but the question hasn't been answered yet.
Why is there a clear reason to use redux-saga?

Saga deals with more complex async flows. Thunk is the simple tool for async redux actions that is probably enough for 90% of projects. I'm a bit biased here because I think saga is often introduced as a shiny new tool into projects without an actual need. I have yet to see a project where they clearly needed saga (and not just thunk).
To your first question: Yes, we could handle async things in components and use async/await to dispatch actions around api requests, but it's bad design, since you're concerning the React component with things it shouldn't care about. It should contain display logic and be testable with only that.
Getting the data from api responses into the redux store should be handled outside of components because it's simply not a concern of the view rendering abstraction layer in your app.

Related

Using both redux and useContext in your react app [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 months ago.
Improve this question
Are there reasons why one would use both redux and useContext in their APP?
If so, what are they? Can you give examples of when to use either?
It is not suggested to use both redux and context api in single app.
Both context and Redux has advantages while developing the application in their own way. Depending upon the use case you need to decide whether to go with context or redux.
Context
If the application is smaller and you need to have a centralized store
that needs to store and access the datas. You can use context api. But the code
will not be that organised when compared to redux.
The below reference will provide you an outline on creating the context,
https://reactjs.org/docs/context.html#reactcreatecontext
The below will provide you the reference on using the context,
https://reactjs.org/docs/hooks-reference.html#usecontext
Redux
If the application you are developing is bigger or enterprise level
application. You can go with redux which provides more optimized code
structure compared to context api.
If you want to integrate and use redux. Use reduxtoolkit for simple and elegant usage.
Use the below link for reference,
https://redux-toolkit.js.org/introduction/getting-started
Both are excellent tools for their own specific niche, Redux is overkill just to pass data from parent to child & Context API truly shines in this case. When you have a lot of dynamic data Redux got your back!
To answer your question Yes you can use them both in the same react app but there is no reason to do so and it is recommended to use one of them depending on your needs

Should I make all API calls with redux, or should I keep them simply in components? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am new at using react/redux. I just started working on a react project, but I don't really know which one do I choose when when it comes to making API calls. Should I do all of API calls with redux or are there cases in which I can handle them using components?
Choosing either one has its own merit. If you want the data from API in global state or for any further use you may need to choose to do your calls in redux.
If the interaction is very less and data is not used any further you can opt to do calls in component
Based on the documentation of redux the best practice would be to make you API calls directly in the "actions" of Redux.
You'll be able to find more information on this page: https://redux.js.org/advanced/async-actions
There is also another thread with a really good answer on how to make a proper API call with Redux:
How to properly make REST calls from ReactJS + Redux application?

Are there some features of redux not provided on react? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I need to start building a data management app, with some CMS like features. The backend is built on Node and frontend needs to work with its API with react.
By using hooks like useReducer and useContext, I can see the core functionality of redux implemented in React core itself.
Are there some functionalities of redux I might miss out on if I choose to not use them at all?
While useReducer sounds like it will behave like Redux does, it is an entirely different concept (well not entirely, but it isn't global), so for the time being just ignore it.
Internally, Redux uses contexts to inject data into components without passing it through the whole tree, so theoretically you could do everything that redux can do with useContext, but you would need tons of your own custom code.
Personally, I think that people are far to quick to jump to using Redux to solve simple tasks. If you have state that isn't often updated, or only updated from a single component high up in the tree, you could easily get away with using a context to deliver the data to deeper components which then access the context through useContext.
The main thing still not in React Hook is in the aspect of useContext is the functionality provided by Redux middleware. In short, if you have a complex app and need to make a series of API calls and those call may have race condition, async/sync mix up together and other complicated situation, you need the help of middleware like saga for manage those API calls in the order / way you want. React Context API still not be able to handle situation like this.

React Redux vs use-global-hook? Which is better? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
Working on my personal project and want to use hooks inside.
I've seen a global state implementation using hooks and my question is:
which one is better to use? Redux vs use-global-hook ?
It seems super easy to manage with the global hook, but what about performance? What are advantages, disadvantages over redux?
I've built apps using both methods and didn't notice any difference in performance; although Redux bindings are supposed to prevent unnecessary renders, I'm not sure how much of an impact this would make on the final performance of the app. Furthermore, Redux offers a time travelling debugger as well as a middleware API. The following article discusses this topic in more depth:
https://frontarm.com/james-k-nelson/when-context-replaces-redux/
If you're planning to build a sizeable project of any complexity, Redux is going to provide a feature-rich store and state management via a unidirectional flow, actions, reducers, middleware, asynchronous thunks, etc. Redux is a mature pattern (based on Flux) with a great deal of "road-testing" and a thriving community. Also with Redux you'll be able to use fully stateful React class components and leverage the component lifecycle.
Global state hooks seem like a great upcoming solution for simpler components and problems where you want to keep your components functional and pure. I don't see any signs the performance would be any different, the only way to tell would be via empirical profiling.

Should I use redux for all the states I'm going to use in my app ? or I have to go along with setState some times? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
I have a big application with different features. What I want to know exactly is that should I overwrite all setState() functions that I have used with redux ? or in some cases I have to use redux instead ?
I mean is it bad to use redux for a simple setSate action ?
It is ultimately up to you, but Redux is pitched as "global state management", which I interpret as "the state of the app".
Generally, the way I would build my state out would be to have Redux handle the data of logical/business structures (e.g. data coming from APIs), and as such I would pass in props to the components that use said data.
Once inside the component, I would use the component state to allow the component to manage its own logic for the things that it is concerned about, such as logic needed to conditionally render things or optimistic updates.
Some have even completely removed Redux from their applications with the advent of GraphQL + Apollo, allowing them to have server-side computations for what the UI would traditionally display (e.g. providing a boolean like hasComment) so that the UI doesn't have to perform that computation (backend is usually faster at these computations, too) and simply state {hasComment && <Comment />}. Another advantage of this approach is that other UIs could use hasComment without needing to implement duplicate logic.

Resources