Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where should be the line to separate stateful and stateless component in React?

React encourages the use of stateless components as much as possible and have a stateful parent component managing them. I understand that this can make the stateless components more reusable, and easy to manage. However, to the extreme, we can always put the state at the top level component, like App.js, and pass down information and callbacks through a long props chain. And if using Flux, the actions can always be dispatched in it too (executed through callbacks).

So I'm wondering what's line to separate stateful and stateless components? And if using Flux, where should the Actions to be dispatched?

--- Add an example ---

Say I have a google docs like web app that have a tool bar and displayed content. I imagine we will have the component structure.

<App>
    <Toolbar />
    <Content />
</App>

The tool bar has buttons that will affect the display content, say the bold text button.

So should the App pass down onButtonPressed callback props to Toolbar and dispatch Actions in itself, or should let the Toolbar to do it itself?

Should the App pass down contentString props to Content, or let Content itself listen to Store changes?

Thanks!

like image 705
YiFeng Avatar asked Sep 09 '15 03:09

YiFeng


People also ask

What is diff between stateful vs stateless React?

In React, a stateful component is a component that holds some state. Stateless components, by contrast, have no state. Note that both types of components can use props. In the example, there are two React components.

When would you use stateful vs stateless components?

Stateful and stateless components have many different names. The literal difference is that one has state, and the other doesn't. That means the stateful components are keeping track of changing data, while stateless components print out what is given to them via props, or they always render the same thing.

How can you make a functional component stateful or stateless?

A stateless component renders output which depends upon props value, but a stateful component render depends upon the value of the state. A functional component is always a stateless component, but the class component can be stateless or stateful.

Which of the following is not required when working with stateless functional components?

Note that the bind keyword isn't necessary for the stateless component.


1 Answers

From my point of view, a simple application could use the pattern of Flux in that way :

  1. Children emit actions.
  2. The application listens to stores and propagates processed data to his children.

With that approach, you have the stateless component, but with a good code organisation without the callback props. But both of your propositions are also correct, it's a decision that you make regarding the size and needs of your application.

If the component that you build will be used outside of your application, don't use flux as much as possible and let the developer choose the wanted approach for his needs.

like image 63
gaelgillard Avatar answered Nov 15 '22 04:11

gaelgillard