Home > Blockchain >  What is different btw useState<string[]>([]); and useState([]);
What is different btw useState<string[]>([]); and useState([]);

Time:07-18

I was browing stackoverflow while looking for the question that I can answer and I came accross this question

This person use something like

  const [nameInput, setNameInput] = useState<string>('');
  const [availableDays, setAvailableDays ] = useState<string[]>([]);

and I normally use

  const [nameInput,setNameInput] = useState('');
  const [availableDays,setAvailableDays] = useState([]);

and sometimes this

  import * as React form 'react';
  const [nameInput,setNameInput] = React.useState('');
  const [availableDays,setAvailableDays] = React.useState([]);

so which is better?

CodePudding user response:

This is typeScript

// the state can hold a value with type string
const [nameInput, setNameInput] = useState<string>('');

// the state can hold an array of strings
 const [availableDays, setAvailableDays ] = useState<string[]>([])

if you try to put an integer into one of them, it will give you an error. IMO, typescript is better since it mitigates the chance of passing values that shouldn't be passed

  • Related