英文:
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 '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;
答案1
得分: 2
这可能是因为以下 eslint 规则 react/no-unstable-nested-components
。
根据 react-navigation
的指南,你可能已经定义了 headerLeft
属性,但上述 eslint 规则与之相矛盾。
矛盾出现在这里:
<Drawer.Screen
name="Article"
component={Article}
options={{
title: 'Timeline',
headerLeft: () => <BackButton />, // <--- 这一行
}}
/>
可能的修复方法:
- 为文件禁用插件:
// eslint-disable react/no-unstable-nested-components
(或者仅为一行禁用) - 将组件定义移到组件外部。
- 编辑
.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:
<Drawer.Screen
name="Article"
component={Article}
options={{
title: 'Timeline',
headerLeft: () => <BackButton />, // <--- this line
}}
/>
Possible fix(es):
- Disable plugin for the file:
// eslint-disable react/no-unstable-nested-components
(or for just one line) - Move component definition out of the component.
- Edit the
.eslintrc
to allow component as props in this rule's settings
...
"react/no-unstable-nested-components": [
"off" | "warn" | "error",
{ "allowAsProps": 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 <BackButton />
, which you're not, then this should work:
<Drawer.Screen
name="Article"
component={Article}
options={{
title: 'Timeline',
headerLeft: BackButton
}}
BackButton
is a function that returns a component, i.e. <BackButton />
. So your anonymous function isn't necessary,
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论