无法在 js react-native 中设置状态
Can't set state in js react-native
在 React Native 中尝试 setState
时出错。
代码
import React from "react";
import { TextInput, Text, View, Button, Alert } from "react-native";
const UselessTextInput = () => {
state = { currentDate: "" };
const setCurentDate = (val) => {
this.setState({currentDate : val});
};
const [value, onChangeText] = React.useState("");
return (
<View>
<Text
style={{
alignSelf: "center",
marginTop: 60,
fontWeight: "bold",
fontSize: "25",
}}
>
BirthReminder
</Text>
<Text style={{ alignSelf: "center", marginTop: 15, fontSize: 15 }}>
Enter your friend's birthdate, please
</Text>
<TextInput
clearTextOnFocus={true}
style={{
height: 40,
borderColor: "gray",
borderWidth: 1,
marginTop: 20,
width: 250,
alignSelf: "center",
}}
onChangeText={(value) => setCurentDate(value)}
value={value}
/>
<Button title="Add to list"></Button>
</View>
);
};
export default UselessTextInput;
错误
TypeError: undefined is not an object (evaluating '_this.setState')
我觉得你有点糊涂了
如果您使用 Class 组件
,请将其替换为语法
state = { currentDate: "" };
const setCurentDate = (val) => {
this.setState({currentDate : val});
};
与:
const [date, setDate] = React.useState();
const setCurentDate = (val) => {
setDate(val);
};
并查看 documentation
this.setState
在功能组件中是不允许的。尝试将 React.useState
用于 currentDate
const [currentDate, setCurrentDate] = React.useState("");
...
const setCurentDate = (val) => {
setCurrentDate(val);
};
使用State Hook
功能组件无法访问 setState
方法但 useState 钩子。
useState 钩子通过定义值的名称来工作,例如foo
后面是 setter。按照惯例,setter 与值的名称同名并带有 set 前缀,即 setFoo
const [foo, setFoo] = useState('hi');
// pass the initial value here -^^-
解决方案
import { useState } from 'react';
import { TextInput } from 'react-native';
const Component = () => {
const [value, setValue] = useState('');
return <TextInput value={value} onChangeText={setValue} />;
};
在 React Native 中尝试 setState
时出错。
代码
import React from "react";
import { TextInput, Text, View, Button, Alert } from "react-native";
const UselessTextInput = () => {
state = { currentDate: "" };
const setCurentDate = (val) => {
this.setState({currentDate : val});
};
const [value, onChangeText] = React.useState("");
return (
<View>
<Text
style={{
alignSelf: "center",
marginTop: 60,
fontWeight: "bold",
fontSize: "25",
}}
>
BirthReminder
</Text>
<Text style={{ alignSelf: "center", marginTop: 15, fontSize: 15 }}>
Enter your friend's birthdate, please
</Text>
<TextInput
clearTextOnFocus={true}
style={{
height: 40,
borderColor: "gray",
borderWidth: 1,
marginTop: 20,
width: 250,
alignSelf: "center",
}}
onChangeText={(value) => setCurentDate(value)}
value={value}
/>
<Button title="Add to list"></Button>
</View>
);
};
export default UselessTextInput;
错误
TypeError: undefined is not an object (evaluating '_this.setState')
我觉得你有点糊涂了
如果您使用 Class 组件
,请将其替换为语法 state = { currentDate: "" };
const setCurentDate = (val) => {
this.setState({currentDate : val});
};
与:
const [date, setDate] = React.useState();
const setCurentDate = (val) => {
setDate(val);
};
并查看 documentation
this.setState
在功能组件中是不允许的。尝试将 React.useState
用于 currentDate
const [currentDate, setCurrentDate] = React.useState("");
...
const setCurentDate = (val) => {
setCurrentDate(val);
};
使用State Hook
功能组件无法访问 setState
方法但 useState 钩子。
useState 钩子通过定义值的名称来工作,例如foo
后面是 setter。按照惯例,setter 与值的名称同名并带有 set 前缀,即 setFoo
const [foo, setFoo] = useState('hi');
// pass the initial value here -^^-
解决方案
import { useState } from 'react';
import { TextInput } from 'react-native';
const Component = () => {
const [value, setValue] = useState('');
return <TextInput value={value} onChangeText={setValue} />;
};