为什么在使用 React 上下文时会出现缺少 <Text> 错误?
Why do I get a missing <Text> error when using react context?
为什么我创建和使用反应上下文的实现会导致与文本相关的错误?
这是我将上下文指定为包装器组件的组件。
import React from 'react';
import { createStackNavigator } from '@react-navigation/stack';
import SignInScreen from '../screens/SignInScreen';
import SignUpScreen from '../screens/SignUpScreen';
import ConfirmEmailScreen from '../screens/ConfirmEmailScreen';
import ForgotPasswordScreen from '../screens/ForgotPasswordScreen';
import NewPasswordScreen from '../screens/NewPasswordScreen';
import { SimpleContextProvider } from '../contexts/SimpleContext';
const Stack = createStackNavigator();
export default function LoginStack(props) {
return (
<SimpleContextProvider>
<Stack.Navigator mode="card" screenOptions={{ headerShown: false }}>
<Stack.Screen name="SignIn" component={SignInScreen} />
<Stack.Screen name="SignUp" component={SignUpScreen} />
<Stack.Screen name="ConfirmEmail" component={ConfirmEmailScreen} />
<Stack.Screen name="ForgotPassword" component={ForgotPasswordScreen} />
<Stack.Screen name="NewPassword" component={NewPasswordScreen} />
<Stack.Screen name="App" component={AppStack} />
</Stack.Navigator>
</SimpleContextProvider>
);
}
这是我的简单上下文文件...SimpleContext.js
import * as React from 'react';
const SimpleContext = React.createContext();
const SimpleContextProvider = ({children}) => {
return <SimpleContext.Provider value={1}> {children} </SimpleContext.Provider>;
};
const useSimple = () => React.useContext(SimpleContext);
export { SimpleContextProvider, useSimple };
为什么我会看到这个错误?
文本字符串必须在组件中呈现?我没有看到任何未包装在组件中的杂散字符串。如果我删除 SimpleContextProvider,此错误就会消失。
以完整描述的名义,我将上下文包装在导航屏幕周围。也许这很重要。
children 周围有空格。它们位于一行的中间,因此在输出中它们将被视为空格。即,这个:> {children} <
需要是这个 >{children}<
。或者,如果将它分成多行,它会起作用,因为行首和行尾的空格将被忽略:
return (
<SimpleContext.Provider value={1}>
{children}
</SimpleContext.Provider>
)
为什么我创建和使用反应上下文的实现会导致与文本相关的错误?
这是我将上下文指定为包装器组件的组件。
import React from 'react';
import { createStackNavigator } from '@react-navigation/stack';
import SignInScreen from '../screens/SignInScreen';
import SignUpScreen from '../screens/SignUpScreen';
import ConfirmEmailScreen from '../screens/ConfirmEmailScreen';
import ForgotPasswordScreen from '../screens/ForgotPasswordScreen';
import NewPasswordScreen from '../screens/NewPasswordScreen';
import { SimpleContextProvider } from '../contexts/SimpleContext';
const Stack = createStackNavigator();
export default function LoginStack(props) {
return (
<SimpleContextProvider>
<Stack.Navigator mode="card" screenOptions={{ headerShown: false }}>
<Stack.Screen name="SignIn" component={SignInScreen} />
<Stack.Screen name="SignUp" component={SignUpScreen} />
<Stack.Screen name="ConfirmEmail" component={ConfirmEmailScreen} />
<Stack.Screen name="ForgotPassword" component={ForgotPasswordScreen} />
<Stack.Screen name="NewPassword" component={NewPasswordScreen} />
<Stack.Screen name="App" component={AppStack} />
</Stack.Navigator>
</SimpleContextProvider>
);
}
这是我的简单上下文文件...SimpleContext.js
import * as React from 'react';
const SimpleContext = React.createContext();
const SimpleContextProvider = ({children}) => {
return <SimpleContext.Provider value={1}> {children} </SimpleContext.Provider>;
};
const useSimple = () => React.useContext(SimpleContext);
export { SimpleContextProvider, useSimple };
为什么我会看到这个错误?
文本字符串必须在组件中呈现?我没有看到任何未包装在组件中的杂散字符串。如果我删除 SimpleContextProvider,此错误就会消失。
以完整描述的名义,我将上下文包装在导航屏幕周围。也许这很重要。
children 周围有空格。它们位于一行的中间,因此在输出中它们将被视为空格。即,这个:> {children} <
需要是这个 >{children}<
。或者,如果将它分成多行,它会起作用,因为行首和行尾的空格将被忽略:
return (
<SimpleContext.Provider value={1}>
{children}
</SimpleContext.Provider>
)