Skip to content

Instantly share code, notes, and snippets.

@JoshuaPoddoku
Last active March 18, 2020 17:55
Show Gist options
  • Save JoshuaPoddoku/38a25a2ad4f782ccedcb1d09db12ea23 to your computer and use it in GitHub Desktop.
Save JoshuaPoddoku/38a25a2ad4f782ccedcb1d09db12ea23 to your computer and use it in GitHub Desktop.
States & Props in React

What are they ?

States

  • A value that changes depending on a user's actions is called state in React.
  • State can be updated.
  • Prevent re-rendering
  • Three steps to use state:
    • Define: State is created using an object in the constructor. constructor(props) { super(props); this.state = {name: 'Ken the Ninja'}; }
    • Display: Since this.state is an object, you can get the value of any property by writing code like this.state.propertyName
    • Update: With the code this.setState({propertyName: valueToUpdate}), the value of state for the specified property changes. setState() method is used.

Props

  • In React, you can pass props, or properties, to child components.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment