Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReactJS - What is difference between component state and class variable?

I think it works differently but I don't know how it works.

1. Using class variable

export default class Test extends Component {
  constructor() {
    this.active = false;
  }

  render() {
    this.active = this.props.name === 'Dan'? true : false;
    return (
      <div>
        {this.active? 'ssup?' : 'noooo'}
      </div>
    );
  }
}

2. Using React component state

export default class Test extends Component {
  constructor() {
    this.state = { active: false };
  }

  render() {
    if(this.props.name === 'Dan') {
      this.setState({active: true});
    }
    return (
      <div>
        {this.active? 'ssup?' : 'noooo'}
      </div>
    );
  }
}

I think it doesn't need to re-render using State if it's only affected by received props.

like image 465
Mook Avatar asked Jan 29 '19 19:01

Mook


People also ask

What is the difference between state and variable in react JS?

In a React component, props are variables passed to it by its parent component. State on the other hand is still variables, but directly initialized and managed by the component. The state can be initialized by props. and any other method in this class can reference the props using this.

Why use state instead of variable in React?

The reason is if you useState it rerenders the view. Variables by themselves only change bits in memory and the state of your app can get out of sync with the view. In both cases a changes on click but only when you use useState the view correctly shows a 's current value. Thanks!

What is the state of a React component?

What Is 'State' in ReactJS? The state is a built-in React object that is used to contain data or information about the component. A component's state can change over time; whenever it changes, the component re-renders.

What is the difference between functional component and class component in React?

A functional component is just a plain JavaScript pure function that accepts props as an argument and returns a React element(JSX). A class component requires you to extend from React. Component and create a render function which returns a React element. There is no render method used in functional components.


1 Answers

The difference between the two is that React will re-render your component when state changes (with this.setState(/*...*/)).

If you update the class variable, React will be unaware of it and won't re-render your component.

Note that what you're achieving in your code requires neither state or class variable. You're simply computing another value directly from the props. A better way to write your component would be like this :

export default class Test extends Component {
  render() {
    const active = this.props.name === 'Dan';
    return (
      <div>
        {active? 'ssup?' : 'noooo'}
      </div>
    );
  }
}
like image 63
debel27 Avatar answered Sep 29 '22 21:09

debel27