React and Redux pattern - reactjs

I'm confused about the right way of using redux and react.
Lets say in my home http://myapp/ I display a list of books, and I populate in my state an array books[] containing title and id. Each line in the home have a link to a page with book details, let's say http://myapp/book/123. When I load that route, I go to the server, fetch the data, create an action and reduce to have the book with id 123 filed with the details, so page can render properly. Is this the correct approach?
An if it is ok, whats if I use a link http://myapp/book/123 directly? My app does not pass trough the home, so initial state is not initialized... I'm sure there is something wrong in my approach, but can't figure out.

I found that Redux complicates things a lot. Since react introduced hooks I prefer to use hooks and simple context. You may find that your application does not need Redux at all. This may help: replace redux react hooks context api

Related

how to use Redux instead for useSate or how to store data using Redux

const[datas,setdatas]=useState("")
i have been using useState for storing variable and while finishing the last task my higher official told me to use Redux to store change and perform action . i read it in blog youtube videos but I can't quite get to know the necessary about redux. can someone suggest me a easy example or solution for me?
Thanking you in advance.
If you have a bunch of nested components in you app, you have to send props every time to the new nested component.
Then you feel like you want your states to be managed globally.
Otherwise you probably have to type same props again and again.
This is why Redux and other state managing modules are needed.
They put your state in a global storage and you can get that state in other component with props drilling. You only need to access global storage to get necessary state.
If your app has few components, redux or other state managing modules won't be necessary.
Redux provides a easy way to pass data through the component tree without having to pass props down manually at every level, and you should only use it for global states.
I think you can keep using useState for local states.
I would like to answer in 3 points
1. Understanding Redux
I found this (three principles) to be the most helpful way to begin understanding Redux.
https://redux.js.org/understanding/thinking-in-redux/three-principles
2. Why you have been asked to use redux?
Considering your senior has asked you to use Redux, I am assuming you are only coding a part of a larger app (already using redux) and the state in question needs to be a "global state" for use by multiple other components. If that's not the case please look at other solutions like react's context provider or simply using local state and passing it down with props where needed.
3. What else do you need to know?
If point 2 holds, I would urge you to check with your senior/team for the existing implementation of redux store, reducers, middleWare and devTools.
Note : I found redux to be a complex tool and could understand and appreciate it only after using it for a while.

React data flow

I am trying to create a blog application and I am a bit confused on the data flow patterns of React.
Should I be trying to use something like Redux to store all of my posts after fetching the posts? Do I use local storage?
How do I then tell a component to render the 'expanded' version of the post? Is it better to re-use a 'post' component or should I just create two seperate components, one for the title and one for the full post?
I know it's two questions in one, but they kind of go together. Thanks in advance
Here's what I've found out about these topics after 6 months into my self-taught React journey.
In my opinion, React built-in features are more than enough to handle state for a small to medium applications (especially if you're working alone as a single developer).
If you turn to Redux right away, you'll have to learn this whole new pattern of handling state in a single immutable store and how to connect your components to it.
Since you will be most likely fetching data asynchronously, you'll need a helper library to work async on Redux: redux-thunk or redux-saga.
So right from the start, you'll have to add:
redux
react-redux
redux-thunk OR redux-saga
That is a lot of documentation to digest. It's perfectly doable, but I couldn't agree more with this quote:
don't solve problems that you don't have
It will be hard to learn those tools, since you've never faced the problems that they solve. Problems that you don't encounter just yet when you're starting to learn React.
Read this (from Redux creator):
https://medium.com/#dan_abramov/you-might-not-need-redux-be46360cf367
So, my recommendation to you:
Learn basic React (with classes):
https://reactjs.org/docs/getting-started.html
Then learn React Hooks (and you can basically forget about class components):
https://reactjs.org/docs/hooks-intro.html
Build your project using only React at first. And see how it goes. Then you can read more about Redux and what it does, and will be able to make a better choice on if you really need it or not.
From what you've told us about your project:
Keep a state for all your posts in a component high in the tree. Maybe inside the <App/> component itself.
Build your functions to fetch and update post data and update the state with the response.
Render how many components as you wish. Displaying full info about the post inside a BlogPostComponent or simplified version inside a BlogPostThumbnailCard with just the thumbnail and the title, for example.
If you want to be ready for the next versions of React and have a shorter code, you should try the hooks and avoid Classes
https://reactjs.org/docs/hooks-intro.html
You can organize your code as you want, but this is more interesting to have scalability with your components and reuse them with different properties

Redux-form is really good? Doesn't it re-render all page in the case when we use react-router?

I got a question while working with React-redux/redux-form.
I build SPA using React-Router.
React-router uses redux as you know.
What I want to know is the render-flow when you change redux-form-field.
1. user inputs text in redux form field
2. redux form updates redux store
3. Then, all components connected with store are re-rendered.
(It means that all page is refreshed in my case, because I use react-router)
React uses virtual-dom, so twinkles can't be notified.
I'm sorry that I can't express my thinking due to lack of English.
I look forward to hearing your opinions

When do I choose React state Vs Redux Store

I've been learning Redux and a part I'm unclear of is, how do I make a determination between using react state vs redux store and then dispatching actions. from my reading so far it looks like I could use React state in place of Redux store and still get things done. I understand the separation of concerns with using Redux store and just having 1 container component and the rest of it as stateless component but how do I make the determination of when to use React state Vs redux store is not very clear to me. Can someone please help?
Thanks!
If the state doesn't need to be shared with other components, or the state doesn't need to be keep when the component is unmounted, then you can just put it in the component's state.
You can think that the Redux store is the database of front-end, if you have something like product data fetched from an API, then the Redux store is the right place; if you have a dropdown component, which takes a isOpen prop, then the parent of that dropdown can just keep dropdownIsOpen as a component state.
For more information, here is the answer from Dan: https://github.com/reactjs/redux/issues/1287
Also you said
only 1 container component and the rest of it as stateless component
This is incorrect. You can have several container components. A container component can also contain another container component.
From book:
First of all, we should always keep in mind that only the minimal
amount of data needed should be put into the state. For example, if we
have to change a label when a button is clicked we should not store
the text of the label, but we should only save a Boolean flag that
tells us if the button has been clicked or not. Secondly, we should
add to the state only the values that we want to update when an event
happens, and for which we want to make the component re-render.
Another way to figure out whether the state is the right place to
store information is to check if the data we are persisting is needed
outside the component itself or by its children. If multiple
components need to keep track of the same information, we should
consider using a state manager like Redux at the application level.
There are a few things that need to be grasp
State of component: If you want to keep state-specific to your HOC then use state. Now in the function component, we use useState.
State of redux: If you want to share data throughout the application use redux state.
Store of redux: It's nothing but a database for the redux library. In the case of redux, we can have only one store. To use multiple stores we can use Flux but it will make things more complicated.
You're absolutely right. Redux (and flux architecture in general) are only formalism tools to help for building large apps. They're not necessary at all.
There's actually an interesting post called You might not need redux by Dan Abramov, the creator of redux that might give you a better answer than I do: https://medium.com/#dan_abramov/you-might-not-need-redux-be46360cf367#.7093fm1z8
If state is needed only for that particular component then prefer to use react state.
For example using state for UI actions
If any state you want to use across the component/project then go with redux state.
For example API response data etc
For detail understanding refer this link
https://spin.atomicobject.com/2017/06/07/react-state-vs-redux-state/

What is the use of Redux-Router in addition to reac-router?

guys. When I go through the documentation of redux-router, I just cannot understand why do we need it?
Here is what the documentation is trying to explain:
React Router is a fantastic routing library, but one downside is that
it abstracts away a very crucial piece of application state — the
current route! This abstraction is super useful for route matching and
rendering, but the API for interacting with the router to 1) trigger
transitions and 2) react to state changes within the component
lifecycle leaves something to be desired.
It turns out we already solved these problems with Flux (and Redux):
We use action creators to trigger state changes, and we use
higher-order components to subscribe to state changes.
This library allows you to keep your router state inside your Redux
store. So getting the current pathname, query, and params is as easy
as selecting any other part of your application state.
As far as I can understand, it is trying to say redux router can keep router state inside redux store so that we can get route info more conveniently.
But within react-router, I can also easily do it.
Like path="messages/:id", I can use this.props.params.id to get the params.
Can someone explain in what scenario redux-router bring its benefit?
Redux (and in general, the flux pattern) is all about having the entire application state stored in one central place. This has the benefit of easier debugging and makes it easier to implement certain features.
If you've ever used the redux-devtools in a react app with react-router, you'll notice that its only of limited use, because you can't replay the entire lifecycle of the application. When the user changes routes, that's not recorded in the redux store. Redux Router keeps the route state in the store.
You could use this for debugging, you could serialise the entire store history and log it elsewhere to replay user sessions. You could hook into it to implement full undo, or log analytics events.
redux-simple-router is a library which could help you understand how to use react router in a redux application.
It is a very simple library stores the URL in redux state and keeps it in sync with any react-router changes.
redux-simple-router compared to redux-router is:
Much smaller and simpler. You don't need to learn another library on top of everything else.
Encourages direct access of react-router APIs. Need server-side rendering, or something else advanced? Just read react-router's docs.
Only stores the current URL and state, whereas redux-router stores the entire location object from react-router.
Follow one of the these 2 examples to get up and running:
redux-simple-router-example
redux-frontend-boilerplate

Resources