Skip to content

Instantly share code, notes, and snippets.

@ross-u
Created May 12, 2020 10:55
Show Gist options
  • Save ross-u/88dfea632290568f404b4f946227d098 to your computer and use it in GitHub Desktop.
Save ross-u/88dfea632290568f404b4f946227d098 to your computer and use it in GitHub Desktop.

React | state ( exercise solution )


Answers:


1. What is the difference between the React's function components and class components ?


Function components

Function components have props object which contains values passed to the component via props/attributes, and they don't have render or lifecycle methods.

This is the reason why they are called "functional stateless components".

Function components return a JSX string.


Class components

Class components have state and props.

Class components have render() method which renders the JSX.


2. What is the component state ?

The state is an object defined inside of the React class component.

React class components have React's built-in method setState() we must use to update the state.

React's built-in setState() method triggers re-rendering of the DOM when state is changed.

Only the class component itself can define the state (object) or change it's existing state through this.setState().


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment