简体   繁体   English

react-native 代码的哪一部分会重新渲染每个更改?

[英]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.我是 react-native 的新手,我想知道它的运行流程。 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? return 语句之前的部分是运行一次还是每次渲染?
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?如果这个组件在另一个组件的返回语句中被调用,state 会在每次渲染时重置吗?

The part outside return will be executed only one time when we call the component. return 外面的部分在我们调用组件时只会执行一次。

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.如果您希望您的代码多次运行,您可以使用 useEffect,它将根据您的需要运行您的代码,因为您将数组中的依赖变量作为 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.是的,因为您调用任何组件的次数将为该组件创建新状态,如果调用它现在将影响该组件以前的 state。 I think I covered you doubt is my short answer, If I left something Please let me know.我想我涵盖了你的疑问是我的简短回答,如果我留下了一些东西,请告诉我。

There are two different types of components:有两种不同类型的组件:

Stateful (class) components and Stateless (function) components (the one you are using).有状态(类)组件和无状态(功能)组件(您正在使用的组件)。

The class components will only execute the render() method every time state changes and function will execute all the code every time you change the state inside it. The class components will only execute the render() method every time state changes and function will execute all the code every time you change the state inside it. You have to know wich is best for your use cases您必须知道最适合您的用例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM