score:6
you can set a string type for it
explicit way:
const [email, setemail] = usestate<string>('')
or implicit way:
const [email, setemail] = usestate('')
or if you want to set number type
const [inputnumber, setinputnumber] = usestate<number>(0);
then in jsx
, do it this way
<input
type="number"
value={inputnumber}
onchange={(val) => {
//some ways to do this
setinputnumber(number(val.target.value));
setinputnumber(+val.target.value);
setinputnumber(parseint(val.target.value,0));
}}
/>
score:1
without any initial argument, the type of email
and setemail
will be undefined.
const [email, setemail]: [undefined, (value: ((prevstate: undefined) => undefined) | undefined) => void] = usestate();
so the first step is to enforce email to be a string by either using usestate("")
or even better usestate<string>("")
.
score:1
initialize the usestate with an empty string will solve.
usestate can infer the type based on your initialstate, thats why initialize with empty string will solve your case.
usestate without initialstate will be undefined, so your type and state is undefined.
if you want your state to have an more complex type, you need to pass a initialstate with defined type:
const initialstate: type = {
// ...
};
const [state, setstate] = usestate(initialstate);
or
pass the type to usestate without set type to initialstate, like: usestate<type>(initialstate);
score:1
you just need to use generic in this case with usestate
hook
simple case
const [email, setemail] = usestate<string>("")
complex case
- declare an interface
interface iuser{
email:string
}
- make
usestate
generic
const [email, setemail] = usestate<iuser>({email: ''})
you can see definitelytyped
Source: stackoverflow.com
Related Query
- How to use useState hook in React with typescript correctly?
- How to use callback with useState hook in react
- How to use React Context with useState hook to share state from different components?
- How do you correctly use React.lazy() in Typescript to import a react component with a generic type parameter?
- How to use React Hook Form with Typescript (Input Component)
- Typescript error when use react useState hook with generics
- how can i pass a useState hook defined with typescript down to another react component?
- How to use refs in React with Typescript
- Set types on useState React Hook with TypeScript
- How to use React useRef hook with typescript?
- How to prevent race conditions with react useState hook
- How to use React with Typescript and SASS
- How to correctly set initial state in React with Typescript without constructor?
- How to use forwardRef with FunctionComponent in React Native using TypeScript
- How to use Material UI custom theme in React with Typescript
- how to use .env file in a react js with typescript project?
- How to use jest.spyOn with React function component using Typescript
- how can I test if useState hook has been called with jest and react testing library?
- How to use the useState hook with asynchronous calls to change an array?
- How does React useState hook work with mutable objects
- React how to use icon inside Textfield Material-UI with TypeScript
- How to use Refs with Material-ui 4, React 16.8, TypeScript 3.5
- How to use Material UI custom variants in React with Typescript
- How to use emotion/styled with React and Typescript
- How to use radium in React typescript (Error: has no properties in common with type 'CSSProperties'. TS2559)?
- How to correctly export SVGs in a React component library with TypeScript
- How to use react-datetime with react + typescript (inside tsx)
- How to use React refs with new Typescript strictPropertyInitialization option?
- How to use AutoPlay methods with React-slick useRef hook and typescript
- How to use useState with Typescript while passing empty array
More Query from same tag
- How to use Tesseract.js in a React app
- Global state in React Native
- Perform a get request from a service worker based from Angular
- Gatsby state updates only on development but not on build (production)
- React array mapping, toggles all drop-downs on click, I want to open the dropdown for the clicked card only
- Set multiple state values with React useState hook
- How to chain action observables to emit synchronously and then emit a final value
- Open <a> tag using JavaScript
- Next.js api route with IP address
- How to create a conditional rendering of Gatsby's Link component and <a> tag for links?
- React-Beautiful-Dnd doesn't rerender after inserting new Draggable
- count filled fields in an nested object js
- How to disable a button when state changes in React
- Move a specific part of text to next line if it have to
- Passing function through props in ReactJS
- React My component does not render on page load
- How to fade in element on scroll using React Hooks/React Spring
- TypeError: Cannot read property 'then' of undefined on webservice
- How to redirect user to url "/home" after some request is complete using react and typescript?
- Page becomes unresponsive and getting 404 error in the console
- How can I reference a DOM element in a function that makes an action call?
- Iterate objects inside objects in react
- Cannot extract data from post request in node
- how to get the id of the card in which the button is selected in REACT
- pass table row object from table and edit it using Redx-Form
- How are Buttons and Popovers connected when using MUI and ReactJS?
- redux-form How to have initial value in the input field
- React this.setState is not a function only on second call of the function
- React - How to redirect to url website with attirbut to
- State in react component won't render