Search code examples
javascriptreactjsreact-nativerenderingjsx

which part of react-native code re-renders every change?


I'm new to react-native and I was wondering about the running flow of it. For example:

  import React, { useState } from 'react';

  function Example() {
    const [count, setCount] = useState(0);

    return (
      <View>
          .
          .
          .
          .
      </View>
    );
  }

does the part before the return statement runs once or every render?
or every time the component gets called?
what if this component got called inside a return statement of another component , does the state gets reset every render?


Solution

  • The part outside return will be executed only one time when we call the component.

    If you want your code to run multiple times you can you useEffect that will run your code according to your need as you pass dependency variable in a array as second argument of useEffect. Yes as how many times you call any component is will create new states for that component, It will now affect previous state of that component if called. I think I covered you doubt is my short answer, If I left something Please let me know.