【问题标题】:Should the main App component be Pure or Stateless?主 App 组件应该是纯的还是无状态的?
【发布时间】:2019-03-11 03:55:57
【问题描述】:

在 React-Native 中,我们应该使用 Pure Component 还是 Stateless Function 作为主组件?

这里有两种方法:

import React from 'react';
import { Provider } from 'react-redux';

import store from './reducers/AppReducers';
import AppRoutes from './routes/AppRoutes';

// Pure Component
class App extends React.PureComponent {
    render() {
        return (
            <Provider store={store}>
                <AppRoutes />
            </Provider>
        );
    }
}

// Stateless Function
const App = () => {
    return (
        <Provider store={store}>
            <AppRoutes />
        </Provider>
    );
};

【问题讨论】:

  • 没关系。只需了解它们之间的差异即可。
  • @emix 我知道区别;我想知道如果我使用无状态函数,是否存在主组件会重新渲染,而不是使用纯组件重新渲染的情况?

标签: react-native optimization


【解决方案1】:

如果您的组件很简单,请使用stateless。对于简单的组件,不需要使用Pure Components

假设您有一个显示文本的组件并将其设为纯组件,则每次重新渲染时,它都会先进行浅层比较。 在这种情况下,重新渲染会比较高效。

如果组件检查浅比较或重新渲染,则由您决定组件的性能是否正常

提示:如果您有一个非常基本的组件,它只显示一些基本内容,请使用stateless

https://medium.com/groww-engineering/stateless-component-vs-pure-component-d2af88a1200b 这里有详细解释。

在你的情况下,我建议 Pure Component 因为它包含你的整个应用程序,并且重新渲染会比浅比较更昂贵

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-19
    • 2023-04-09
    • 2019-10-13
    • 1970-01-01
    • 1970-01-01
    • 2014-03-04
    • 1970-01-01
    • 2018-04-24
    相关资源
    最近更新 更多