Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6287

Typescript/React-Native: Argument of type 'string | null' is not assignable to parameter of type 'SetStateAction'

$
0
0

I am learning typescript with React-Native and I want to use AsyncStorage library. When setting my state after reading value from the storage, I got this problem:

const value: string | null Argument of type 'string | null' is notassignable to parameter of type 'SetStateAction<never[]>'. Type'null' is not assignable to type 'SetStateAction<never[]>'

The App is showing the value but I want to learn how to solve the problem. I guess I have to assign a type to the useState, but do not know how to assign SetStateAction type.

How do I set the type to the useState to solve this problem?

This is the sample code:

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;

Thank you.


Viewing all articles
Browse latest Browse all 6287

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>