Should I make all API calls with redux, or should I keep them simply in components? [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 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?

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

How to listen for changes with react and firebase? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have two separate projects one is a react application and the other one is for my firestore API that I have functions setup. I call these functions in my react application. I have them separated since the firebase package is very large.
I am looking to figure out how to have my data in real time. I know how to setup snapshots in my functions. So I am wondering if I could setup a function that pings my react application(that could be listener for) that something has updated.
Check out https://github.com/tylermcginnis/re-base - it allows you to persist your data in the React State mirroring any changes that are made in your database. It was made specifically for Firebase + React.js.
There are endpoints (amongst many others) namely syncState which is a two-way bind to state and bindToState which is a one-way bind to state.
Hope this helps!

Why use redux-saga? [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'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.

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.

How to populate list from api and pass data to new page when item is clicked?(react native) [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 6 years ago.
Improve this question
I am very new to react native and am trying to populate a list with an image and a heading using a fetch() call from a web service . I need this list to populate as soon as the page is displayed. I am struggling to find the right method/event to put my logic in .
Also i have a separate page to see the detailed article. Is there a clean way to pass on information to this page so i can avoid making another api call or a messy global variable.
You should read up on Redux. It might seem a bit hard to understand while reading but the implementation is pretty simple and straight forward.
Also, take a look at this article by Dan Abramov, the main contributor of Redux, talking about Presentational and Container components.

Resources