Home  >  Article  >  Web Front-end  >  What is the difference between props and state in React?

What is the difference between props and state in React?

青灯夜游
青灯夜游Original
2020-11-12 17:05:143531browse

Difference: 1. Props are passed to the component (similar to the formal parameters of a function), while state is managed by the component itself inside the component (similar to a variable declared within a function); 2. Props cannot be modified, but state is changeable and can be modified.

What is the difference between props and state in React?

(Related recommendations: React video tutorial)

Developing react components, we most commonly use The two things that cause component rendering may be state and props, so what is the difference between them?

props:

  • props: The props of the function component are the input parameter components of the function
  • Class component: this.props contains the props called or defined by the component

state:

  • The state in the component contains data that may change at any time.
  • state is defined by the user and is an ordinary JavaScript object

Difference:

  • props are passed to the component (similar to function parameters), and state is managed inside the component by the component itself (similar to variables declared within a function)

  • props cannot be modified, all React components must protect their props from being modified like pure functions

  • State is created in the component, usually initialized in the constructor

  • State is changeable and can be modified. Each setState is updated asynchronously

For more programming-related knowledge, please visit:Programming Learning Course! !

The above is the detailed content of What is the difference between props and state in React?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn