React native 尝试导出函数时对本机屏幕作出反应
新的反应(和本地),我试图修改。我正在尝试将React native 尝试导出函数时对本机屏幕作出反应,react-native,React Native,新的反应(和本地),我试图修改。我正在尝试将符号屏幕移动到它自己的文件中,并从默认应用程序文件调用它,下面是发生错误的我的符号屏幕文件: import * as React from 'react'; import { Button, View, TextInput } from 'react-native'; const context = React.createContext(); export function SignInScreen() { const [username,
符号屏幕
移动到它自己的文件中,并从默认应用程序文件调用它,下面是发生错误的我的符号屏幕文件:
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>
);
}
import*as React from'React';
从“react native”导入{按钮、视图、文本输入};
const context=React.createContext();
导出函数符号屏幕(){
const[username,setUsername]=React.useState(“”);
const[password,setPassword]=React.useState(“”);
console.log(上下文);
const{signIn}=React.useContext(context);//在这里爆炸
返回(
登录({username,password})}/>
);
}
通过
我得到一个错误:
无法在SignInScreen读取未定义的属性“signIn”
我假设这只是一个我不理解的封装(或语法或范围)问题 您需要提供
登录
功能的实现。您可以在调用createContext
时执行此操作,也可以将其作为该上下文的提供程序
值传递(假设您有一个,否则这是另一个问题)
const context=createContext({
登录:({用户名,密码})=>{
console.log(用户名、密码);
}
});
我建议你读一读这篇文章。谢谢,自从发帖以来,我一直沉浸在“反应本族语”中,现在我不得不对自己的问题发笑了。