eslint-disable-next-line react/no-unstable-nested-components

huangapple go评论70阅读模式
英文:

React native eslint-disable-next-line react/no-unstable-nested-components

问题

以下是翻译好的代码部分:

我在headerLeft中使用自定义返回按钮我已启用ESLINT它显示以下警告

*不要在渲染期间定义组件React将在每次渲染时看到新的组件类型并销毁整个子树的DOM节点和状态https://reactjs.org/docs/reconciliation.html#elements-of-different-types)。而是,将此组件定义移到父组件“App”之外并将数据传递为props。如果要允许在props中创建组件,请将allowAsProps选项设置为true。eslintreact/no-unstable-nested-components*

> App.tsx

/**
 * 示例React Native应用
 * https://github.com/facebook/react-native
 *
 * @格式
 */

import React from 'react';
import { NavigationContainer } from '@react-navigation/native';
import { createDrawerNavigator } from '@react-navigation/drawer';
import { StatusBar, useColorScheme } from 'react-native';

import { Colors } from 'react-native/Libraries/NewAppScreen';

import { useNavigation } from '@react-navigation/native';

import { DrawerContent } from './src/navigation/drawer-content';
import { Button } from 'react-native-paper';

import Feed from './src/screens/feed';
import Article from './src/screens/article';

const Drawer = createDrawerNavigator();

const CustomDrawer = (props: any) => {
  return <DrawerContent {...props} />;
};

const BackButton = () => {
  const navigation = useNavigation();
  return (
    <Button
      icon="arrow-left-thick"
      onPress={() => navigation.goBack()}
      children={undefined}
    />
  );
};

function App(): JSX.Element {
  const isDarkMode = useColorScheme() === 'dark';

  const backgroundStyle = {
    backgroundColor: isDarkMode ? Colors.darker : Colors.lighter,
  };

  return (
    <NavigationContainer>
      {/* <SafeAreaView style={backgroundStyle}> */}
      <StatusBar
        barStyle={isDarkMode ? 'light-content' : 'dark-content'}
        backgroundColor={backgroundStyle.backgroundColor}
      />
      <Drawer.Navigator drawerContent={CustomDrawer} initialRouteName="Feed">
        <Drawer.Screen name="Feed" component={Feed} />
        <Drawer.Screen
          name="Article"
          component={Article}
          options={{
            title: 'Timeline',
            headerLeft: () => <BackButton />,
          }}
        />
      </Drawer.Navigator>
    </NavigationContainer>
  );
}

export default App;
英文:

I am using a custom back button in the headerLeft. I have enabled ESLINT, and it is showing the below warning.

Do not define components during render. React will see a new component type on every render and destroy the entire subtree’s DOM nodes and state (https://reactjs.org/docs/reconciliation.html#elements-of-different-types). Instead, move this component definition out of the parent component “App” and pass data as props. If you want to allow component creation in props, set allowAsProps option to true.eslintreact/no-unstable-nested-components

> App.tsx

/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
*/
import React from &#39;react&#39;;
import {NavigationContainer} from &#39;@react-navigation/native&#39;;
import {createDrawerNavigator} from &#39;@react-navigation/drawer&#39;;
import {StatusBar, useColorScheme} from &#39;react-native&#39;;
import {Colors} from &#39;react-native/Libraries/NewAppScreen&#39;;
import {useNavigation} from &#39;@react-navigation/native&#39;;
import {DrawerContent} from &#39;./src/navigation/drawer-content&#39;;
import {Button} from &#39;react-native-paper&#39;;
import Feed from &#39;./src/screens/feed&#39;;
import Article from &#39;./src/screens/article&#39;;
const Drawer = createDrawerNavigator();
const CustomDrawer = (props: any) =&gt; {
return &lt;DrawerContent {...props} /&gt;;
};
const BackButton = () =&gt; {
const navigation = useNavigation();
return (
&lt;Button
icon=&quot;arrow-left-thick&quot;
onPress={() =&gt; navigation.goBack()}
children={undefined}
/&gt;
);
};
function App(): JSX.Element {
const isDarkMode = useColorScheme() === &#39;dark&#39;;
const backgroundStyle = {
backgroundColor: isDarkMode ? Colors.darker : Colors.lighter,
};
return (
&lt;NavigationContainer&gt;
{/* &lt;SafeAreaView style={backgroundStyle}&gt; */}
&lt;StatusBar
barStyle={isDarkMode ? &#39;light-content&#39; : &#39;dark-content&#39;}
backgroundColor={backgroundStyle.backgroundColor}
/&gt;
&lt;Drawer.Navigator drawerContent={CustomDrawer} initialRouteName=&quot;Feed&quot;&gt;
&lt;Drawer.Screen name=&quot;Feed&quot; component={Feed} /&gt;
&lt;Drawer.Screen
name=&quot;Article&quot;
component={Article}
options={{
title: &#39;Timeline&#39;,
headerLeft: () =&gt; &lt;BackButton /&gt;,
}}
/&gt;
&lt;/Drawer.Navigator&gt;
&lt;/NavigationContainer&gt;
);
}
export default App;

答案1

得分: 2

这可能是因为以下 eslint 规则 react/no-unstable-nested-components

根据 react-navigation指南,你可能已经定义了 headerLeft 属性,但上述 eslint 规则与之相矛盾。

矛盾出现在这里:

&lt;Drawer.Screen
  name=&quot;Article&quot;
  component={Article}
  options={{
    title: &#39;Timeline&#39;,
    headerLeft: () =&gt; &lt;BackButton /&gt;, // &lt;--- 这一行
  }}
/&gt;

可能的修复方法:

  1. 为文件禁用插件:// eslint-disable react/no-unstable-nested-components(或者仅为一行禁用)
  2. 将组件定义移到组件外部。
  3. 编辑 .eslintrc 以允许此规则设置中的组件作为属性
...
"react/no-unstable-nested-components": [
  "off" | "warn" | "error",
  { "allowAsProps": true | false }
]
...
英文:

It could be because of the following eslint rule react/no-unstable-nested-components.

I guess according to react-navigation's guide you have defined the headerLeft prop but the above eslint rule contradicts that.

Contradiction occurred here:

&lt;Drawer.Screen
  name=&quot;Article&quot;
  component={Article}
  options={{
    title: &#39;Timeline&#39;,
    headerLeft: () =&gt; &lt;BackButton /&gt;, // &lt;--- this line
  }}
/&gt;

Possible fix(es):

  1. Disable plugin for the file: // eslint-disable react/no-unstable-nested-components (or for just one line)
  2. Move component definition out of the component.
  3. Edit the .eslintrc to allow component as props in this rule's settings
...
&quot;react/no-unstable-nested-components&quot;: [
 &quot;off&quot; | &quot;warn&quot; | &quot;error&quot;,
 { &quot;allowAsProps&quot;: true | false }
]
...

答案2

得分: 0

如果您没有向<BackButton />传递任何属性,您确实没有,那么这应该可以工作:

<Drawer.Screen
  name="Article"
  component={Article}
  options={{
    title: 'Timeline',
    headerLeft: BackButton
  }}

BackButton是一个返回组件的函数,即<BackButton />。所以您的匿名函数是不必要的。

英文:

If you're not passing any props to &lt;BackButton /&gt;, which you're not, then this should work:

&lt;Drawer.Screen
  name=&quot;Article&quot;
  component={Article}
  options={{
    title: &#39;Timeline&#39;,
    headerLeft: BackButton
  }}

BackButton is a function that returns a component, i.e. &lt;BackButton /&gt;. So your anonymous function isn't necessary,

huangapple
  • 本文由 发表于 2023年2月18日 20:41:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75493412.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定