score:98
Accepted answer
const [user, setuser] = usestate(null);
since you havn't given this a type, typescript has to try to infer it. it sees you passed in a null, so it assumes this state is (and always will be) null. instead, you need to specify the type, as in:
interface userdata {
username: string;
password: string;
prevstate: null
}
//...
const [user, setuser] = usestate<userdata | null>(null);
score:25
if you fetching data from api you need this:
import react, { usestate, useeffect } from 'react'
import axios from 'axios';
const mycomponent = () => {
const [data, setdata] = usestate<any | null>(null);
const fetchdata = () => {
(...) //axios.get() or async if u await data.
setdata(fetchdata)
}
}
useeffect(() =>{
fetchdata()
}, [])
Source: stackoverflow.com
Related Query
- React Typescript - Argument of type is not assignable to parameter of type
- React js Typescript Argument of type 'string' is not assignable to parameter of type 'SetStateAction<number>'
- TypeScript w/ React - Argument of type 'string' is not assignable to parameter of type 'keyof T'
- Typescript React type argument of type not assignable to parameter of type
- Typescript error on React Component: Argument of type 'Element' is not assignable to parameter of type
- Argument of type 'File' is not assignable to parameter of type 'never'. React with TypeScript
- How to fix React TypeScript error: Argument of type '""' is not assignable to parameter of type 'SetStateAction<undefined>'
- react usestate hooks error: argument of type 'yyy' is not assignable to parameter of type 'setstateaction<xx>'
- Typescript React/Redux : Argument of type 'typeof MyClass' is not assignable to parameter of type 'ComponentType<...'
- React with Typescript: Argument of type 'never[]' is not assignable to parameter of type 'StateProperties | (() => StateProperties)'
- React TypeScript: Argument is not assignable to parameter of type 'never'
- React Typescript: Argument of type '{ [x: number]: any; }' is not assignable to parameter of type
- React onclick Argument of type 'EventTarget' is not assignable to parameter of type 'Node'
- Using state and includes in React gives Argument of type 'number' is not assignable to parameter of type 'never' error
- Argument of type 'never[]' is not assignable to parameter of type 'never' when declaring a type in Typescript
- Typescript Error: Argument of type 'NodeListOf<HTMLInputElement> | undefined' is not assignable to parameter of type 'Iterable<HTMLInputElement> ..."
- Stateful Component with Typescript and react-redux: Argument of type 'typeof MyClass' is not assignable to parameter of type Component
- Typescript complaining about React useState setter `Argument of type 'Dispatch<SetStateAction<never[]>>' is not assignable to parameter of
- React and Typescript: Argument of type is not assignable to parameter of type 'EventListenerOrEventListenerObject'
- Using EmailJS in TypeScript for a contact form, Argument of type 'EventTarget' is not assignable to parameter of type... .ts(2345)
- TypeScript - Argument of type 'String' is not assignable to parameter of type 'string'
- Argument of type '(props: ITableProps) => JSX.Element' is not assignable to parameter of type ... - React HOC
- Argument of type is not assignable to parameter of type 'SetStateAction<never[]>'. in React
- Argument of type is not assignable to parameter of type Typescript Error
- ReactJS Typescript Argument of type '{ keyPrefix: string; }' is not assignable to parameter of type string
- React Typescript: Argument of type is not assignable to parameter of type
- How to fix the error argument of type string or undefined is not assignable to parameter of type string using typescript and react?
- TypeScript error Argument of type is not assignable to parameter of type Appstate despite having all necessary types included
- TS and React State - Argument of type 'RangeModifier' is not assignable to parameter of type
- React typescript is not assignable to parameter of type
More Query from same tag
- Keys vs shouldComponentUpdate, how are they related, are they not the same?
- Electron with React and Firebase login pop-up issue
- Webpack - React, How to pass data from script tag
- React - use state on an empty array
- How to limited the countries list in npm react-phone-number-input package
- Why is the timer not working correctly when updating data in React?
- Pass setState to axios inside handler function (React)
- GraphQL rate limit on mutations
- Calling a seperate function from within useEffect in react
- Adding values of redux-form field array
- Spread operator error in return value, parse error
- use date from fetch object to create link for fetch another object in react
- Error: useRoutes() may be used only in the context of a <Router> component
- React-draggable npm package prevents clicking inside input field
- Array include by
- How return a setable const in React
- I am facing a problem in JavaScript/ReactJS
- How can I send form error as a param to parent component?
- JSDoc does not work in local package in a Lerna monorepo project using webpack dev server but works when released to package registry
- React recompose componentFromStream update not triggers
- Gulp + browserify + babelify: "Unexpected token" for JSX code
- store token in cookie or local storage in reactjs
- Redux/Java: Managing normlized data & multiple model representations per entity
- How to use justify content property in react native?
- Trigger props.onChange() when onBlur() on TextField
- Why is videojs disabled when i close popup?
- React-Select search input -Turkish lowercase "i" character does not work to get results (words) starting with "İ"
- React InstantSearch : onSearchStateChange get the number of returned hits
- CORS error on localhost, Is that a normal?
- ReactStrap: DropDown items appear transparent on phone