Typescript/React-Native: 'string | null' 类型的参数不可分配给 'SetStateAction<never[]>' 类型的参数
Typescript/React-Native: Argument of type 'string | null' is not assignable to parameter of type 'SetStateAction<never[]>'
我正在使用 React-Native
学习 typescript
,我想使用 AsyncStorage
库。从存储中读取值后设置我的状态时,我遇到了这个问题:
const value: string | null Argument of type 'string | null' is not
assignable to parameter of type 'SetStateAction<never[]>'. Type
'null' is not assignable to type 'SetStateAction<never[]>'
该应用程序正在显示价值,但我想了解如何解决问题。我想我必须将 type
分配给 useState
,但不知道如何分配 SetStateAction
类型。
如何将类型设置为useState
来解决这个问题?
这是示例代码:
import React, {useEffect, useState} from 'react';
import {Text, View} from 'react-native';
import AsyncStorage from '@react-native-async-storage/async-storage';
const App = () => {
const [value, setValue] = useState([]);
async function saveValue(key: string, value: string) {
await AsyncStorage.setItem(key, value);
}
async function readValue(key: string) {
const value = await AsyncStorage.getItem(key);
setValue(value);
}
useEffect(() => {
saveValue('123', 'Hello world! ');
readValue('123');
}, []);
return (
<View>
<Text>{value}</Text>
</View>
);
};
export default App;
谢谢。
使用 const [value,setValue] = useState<string | null>('');
解决了@Mic Fung 所建议的问题。这是结果代码:
const App = () => {
const [value, setValue] = useState<string | null>('');
async function saveValue(key: string, value: string) {
await AsyncStorage.setItem(key, value);
}
async function readValue(key: string) {
const value = await AsyncStorage.getItem(key);
setValue(value);
}
useEffect(() => {
saveValue('123', 'Hello world! ');
readValue('123');
}, []);
return (
<View>
<Text>{value}</Text>
</View>
);
};
世界,您好!
我正在使用 React-Native
学习 typescript
,我想使用 AsyncStorage
库。从存储中读取值后设置我的状态时,我遇到了这个问题:
const value: string | null Argument of type 'string | null' is not assignable to parameter of type 'SetStateAction<never[]>'. Type 'null' is not assignable to type 'SetStateAction<never[]>'
该应用程序正在显示价值,但我想了解如何解决问题。我想我必须将 type
分配给 useState
,但不知道如何分配 SetStateAction
类型。
如何将类型设置为useState
来解决这个问题?
这是示例代码:
import React, {useEffect, useState} from 'react';
import {Text, View} from 'react-native';
import AsyncStorage from '@react-native-async-storage/async-storage';
const App = () => {
const [value, setValue] = useState([]);
async function saveValue(key: string, value: string) {
await AsyncStorage.setItem(key, value);
}
async function readValue(key: string) {
const value = await AsyncStorage.getItem(key);
setValue(value);
}
useEffect(() => {
saveValue('123', 'Hello world! ');
readValue('123');
}, []);
return (
<View>
<Text>{value}</Text>
</View>
);
};
export default App;
谢谢。
使用 const [value,setValue] = useState<string | null>('');
解决了@Mic Fung 所建议的问题。这是结果代码:
const App = () => {
const [value, setValue] = useState<string | null>('');
async function saveValue(key: string, value: string) {
await AsyncStorage.setItem(key, value);
}
async function readValue(key: string) {
const value = await AsyncStorage.getItem(key);
setValue(value);
}
useEffect(() => {
saveValue('123', 'Hello world! ');
readValue('123');
}, []);
return (
<View>
<Text>{value}</Text>
</View>
);
};