Skip to content

Instantly share code, notes, and snippets.

@sandrabosk
Last active March 31, 2020 21:49
Show Gist options
  • Save sandrabosk/b1196cce4f9bf9a2a7cb2429fbb2d5c7 to your computer and use it in GitHub Desktop.
Save sandrabosk/b1196cce4f9bf9a2a7cb2429fbb2d5c7 to your computer and use it in GitHub Desktop.

Types of components in React - cheatsheet

Function component

  • [for now] has no state
  • has props (props.something or ({something}) when accessing it)
import React from 'react';

// function component - traditional
function SomeComponent([props]){
   return(
     <></>
   )
}

export default SomeComponent;
import React from 'react';

// function component - ES6 way
const SomeComponent = ([props]) => {
   return(
     <></>
   )
}

export default SomeComponent;

Class component

  • has state (this.state when accessing it)
  • has props (this.props when accessing it)
import React from 'react';

// class component
class SomeComponent extends React.Component {

  state={
    someKey: value
  };
  
  someMethodToManipulateState(){
    this.setState({
      someKey: newValue
    })
  }
  render(){
     console.log(this.[props])
     console.log(this.state)
    return(
      <></>
    )
  }
}

export default SomeComponent;
  • [props] is in brackets since component could but it doesn’t have to have them.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment