Skip to content

Instantly share code, notes, and snippets.

@theabhayprajapati
Created October 14, 2022 05:08
Show Gist options
  • Save theabhayprajapati/320aac279f290b700c06f695367a8588 to your computer and use it in GitHub Desktop.
Save theabhayprajapati/320aac279f290b700c06f695367a8588 to your computer and use it in GitHub Desktop.

setState Methods

setState is having 2 params first is for updating the state and the second is the callback function which only update if this setState is updated and rendered into the DOM.

    () => {
    this.setState((state, props) => {
        return {name: "Abhay"}
    }, () => {
        return console.log(this.state.name)
    })
}

Why Key is Important.

in react key is necessary has it help's it analysis and update what is required, which means it will only update those methods with that specific key else not. if comes for everything from state Objs to .map() methods

ComponentDidMount

with react there and another methods componentDidMount which run after's the render() is ran which means componenDidMount update the renders() after it's render().

Flow Of Render.

  1. constructor() which create initialstates or state for the application.
  2. render() renders the UI on the screen.
  3. ComponentDidMount() method.

Simple Optimization Methods.

  1. when you are using callback function inside the render() methods that callback function will be created when ever the render() is called and be deleted after the work is done, and if agian is render() is called it will be created again so if the callback isn't dynamic you have create a separate function for it, which will created only once in memory and be used again and again.

When does the components renders()

  1. Components render when on mount(), when state changes and when the props changes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment