简体   繁体   中英

Required type to be defined for useState in typescript

what is the required type to be defined for dataList in typescript please refer below snippet

const [dataList, setDataList] = useState([]); 
      dataList= [
                 [{header:"first",tail:"three"},{header:"second",tail:"four"}], 
                 [{header:"first",tail:"three"},{header:"second",tail:"four"}] 
                ];

You will need to provide the types for the dataList state, as TypeScript is unable to infer it. This can be done via interfaces or type alias.

interface Data {
  header: string;
  tail: string; 
}

And this is how you can use it on the useState generic parameter, whereby dataList is a multidimensional array of the Data interface that is defined above:

const [dataList, setDataList] = useState<Data[][]>([]); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM