ReactJS difference between stateful and stateless
Asked Answered
D

4

34

I am trying to understand the exact difference between React's stateful and stateless components. Ok, stateless components just do something, but remember nothing, while stateful components may do the same, but they remember stuff within this.state. That's the theory.

But now, checking on how to show this using code, I have a little trouble making the difference. Am I right with the following two examples? The only difference really is the definition of the getInitialState function.

Example of a stateless component:

var React = require('react');

var Header = React.createClass({
    render: function() {
        return(
            <img src={'mypicture.png'} />
        );
    }
});

module.exports = Header;

Example of a stateful component:

var React = require('react');

var Header = React.createClass({

    getInitialState: function() {
        return {
            someVariable: "I remember something"
        };
    },

    render: function() {
        return(
            <img src={'mypicture.png'} />
        );
    }
});

module.exports = Header;
Dixil answered 29/12, 2015 at 14:4 Comment(0)
F
33

Yes, that is sort of the difference. Except with the stateful component you can also change the state, using this.setState for example:

var React = require('react');

var Header = React.createClass({

    getInitialState: function() {
        return {
            imageSource: "mypicture.png"
        };
    },

    changeImage: function() {

        this.setState({imageSource: "differentpicture.png"});
    },

    render: function() {
        return(
            <img src={this.state.imageSource} onClick={this.changeImage.bind(this)} />
        );
    }
});

module.exports = Header;

So, in the example above, the changeImage manages the state of the component (which would also cause all child/dependent components to be re-rendered).

Somewhere in the application, you need to bind data, or remember things. Stateless components are dumb (and that is good), they cannot remember and they cannot give context to other parts of the UI. Stateful components provide the necessary context glue.

On the other hand, stateless components just get passed context (usually through this.props:

var React = require('react');

var Header = React.createClass({
    render: function() {
        return(
            <img src={this.props.imageSource} />
        );
    }
});

ReactDOM.render(<Header imageSource="myImage.png"/>, document.body);

In the example above, you can see that during the render, imageSource is passed in as an attribute and is then added to the stateless components this.props object.

Family answered 29/12, 2015 at 15:23 Comment(0)
I
8

Functional Component or Stateless component

  1. Functional component is like pure function in JavaScript.
  2. Functional component is also called as a stateless component.
  3. The functional component only receives props from parent component and return you JSX elements.
  4. The functional component doesn’t play with any lifecycle methods of React and doesn’t play with the component state.

Class component or statefull component

  1. React class component is called as a stateful component.
  2. Stateful component plays with all life cycle methods of React.
  3. This component will modify state.

That’s the major differences

If you have knowledge about pure function in JavaScript then you can understand functional or stateless component easily.

Indifferentism answered 17/10, 2018 at 13:41 Comment(2)
Your last line is a very good statement, and plotting that pure, stateless and functional are not exactly but same.Circumspect
which is the best ?Trieste
F
4

In a simple definition it can be explained as

If the behaviour of a component is dependent on the state of the component then it can be termed as stateful component and if behaviour is independent of its state then it can be a stateless component.

When something is “stateful”, it is a central point that stores information in memory about the app/component’s state. When something is “stateless”, it calculates its internal state but it never directly mutates it.

The stateless components are sometimes referred as dumb components

The main advantage stateless over stateful component is scalability and reusability.

Now you can check the example components in @Davin Tryon Answer

Functionary answered 17/11, 2017 at 3:19 Comment(0)
U
0

State Full

  1. Stateful component plays with all life cycle methods of React.
  2. This component will modify the state.
  3. We can access all the method inside state full component

State Less

  1. The stateless component only receives props from the parent component and returns you JSX elements.
  2. The stateless component doesn’t play with any lifecycle methods of React and doesn’t play with the component state.
export default function example(props) {
         return (
            {props.person.firstName}
         )
}
Unity answered 31/5, 2021 at 13:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.