react-native - React Native screen trying to export function
问题描述
New to React (and Native), I'm trying to modify this working Snack. I'm trying to move the SignInScreen
to its own file and call it from the default App file, here is my SignInScreen file where the error is happening:
import * as React from 'react';
import { Button, View, TextInput } from 'react-native';
const context = React.createContext();
export function SignInScreen() {
const [username, setUsername] = React.useState('');
const [password, setPassword] = React.useState('');
console.log(context);
const {signIn} = React.useContext(context); //blows up here
return (
<View>
<TextInput
placeholder="Username"
value={username}
onChangeText={setUsername}
/>
<TextInput
placeholder="Password"
value={password}
onChangeText={setPassword}
secureTextEntry
/>
<Button title="Sign in" onPress={() => signIn({ username, password })} />
</View>
);
}
Called via <Stack.Screen name="SignIn" component={SignInScreen}>
I'm getting an error:
Cannot read property 'signIn' of undefined at SignInScreen
I'm assuming this just an encapsulation (or syntax or scope) issue I don't understand.
解决方案
您需要提供该signIn
功能的实现。您可以在调用时执行此操作,也可以createContext
将其作为该Provider
上下文的值传递(假设您有一个,否则这是另一个问题)。
const context = createContext({
signIn : ({ username, password }) => {
console.log(username, password);
}
});
我建议你稍微阅读一下文档。
推荐阅读
- azure - 数据工厂中的自定义验证错误报告
- r - 重命名向量中的所有列
- php - InterAkt KT 函数的 PHP 会话 ID 问题
- node.js - Node.js:当我尝试启动我的应用程序时,我的服务器请求 hot-update.js
- python - 如何在pygame中退出窗口?
- haskell - 阐明 Haskell 中的数据构造函数
- android - AOSP 系统服务在尝试从 SystemServer 添加服务时出现 avc denied 错误
- python - 币安 SPOT 交易历史中未显示订单市场买卖
- javascript - 观看:嵌套对象为 true - NuxtJS (Vue),最新版本
- typescript - Yarn 2 PnP、VSCode 和 @types