Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 通过typescript';s接口来反应状态_Reactjs_Typescript_State - Fatal编程技术网

Reactjs 通过typescript';s接口来反应状态

Reactjs 通过typescript';s接口来反应状态,reactjs,typescript,state,Reactjs,Typescript,State,我是打字新手。我想将typescript的接口传递给react state,然后呈现该状态。但我不知道如何将接口值传递给react state import React, { useState } from 'react' export interface Person { name: name age: age } type name = "John" type age = 30 const Test = () => { const [state, setstate]

我是打字新手。我想将typescript的接口传递给react state,然后呈现该状态。但我不知道如何将接口值传递给react state

import React, { useState } from 'react'

export interface Person {
  name: name
  age: age
}

type name = "John"
type age = 30
const Test = () => {

  const [state, setstate] = useState() //I want to pass the interface in here
  return (
    <div>
    <p> He is {state.name} and he is {state.age} years old</p> 
    </div>
  )
}
export default Test;
import React,{useState}来自“React”
出口接口人员{
姓名:姓名
年龄:年龄
}
键入name=“John”
类型年龄=30
常数测试=()=>{
const[state,setstate]=useState()//我想在这里传递接口
返回(
他是{state.name},他是{state.age}岁

) } 导出默认测试;
您可以使用useState定义接口类型,如

const [state, setstate] = useState<Person>({name: "John", age: 30 });
const[state,setstate]=useState({name:“John”,年龄:30});
请确保根据Person type属性提供InitialState。如果你定义了一个空对象,你可以这样写

const [state, setstate] = useState<Person>(Object);
const[state,setstate]=useState(对象);
还可以通过显式定义useState类型将其指定为null或未定义

const [state, setstate] = useState<Person | undefined>();
const[state,setstate]=useState();

const[state,setstate]=useState(null);

创建一个与接口匹配的对象,并将其传递到
useState
,如下所示:

const [state, setState] = useState({name: "John", age: 30});
您还可以明确说明该状态变量的类型,因为
useState
是泛型的:

const [state, setState] = useState<Person>({name: "John", age: 30});
在这种情况下,由于类型将是
null
undefined
,如果它只是从传递到
useState
的内容推断出来的,因此在调用中需要泛型类型参数


对我来说,这个概念是打字脚本的基础。与其说某物是一种类型,不如说它是Java中的类型,而是某物与一种类型相匹配。这是非常有效的类型脚本:

interface A {
    name: string;
    age: number;
}
interface B {
    name: string;
    age: number;
}
let a: A = {name: "Joe", age: 27};
let b: B;
b = a;
无论
b
被声明为类型
b
还是
a
被声明为类型
a
,您都可以执行
b=aa
的类型在结构上与
b
的类型兼容(在本例中,它们是相同的)

这也是完全正确的:

interface A {
    name: string;
    age: number;
    rank: string;
}
interface B {
    name: string;
    age: number;
}
let a: A = {name: "Joe", age: 27, rank: "Junior Petty Officer"};
let b: B;
b = a;
a
的类型具有
b
的类型不具有的属性(
rank
)是可以的。它仍然与
b
的类型兼容。

(啊!当然这是一个重复的问题。因为我已经回答了,所以我将它标记为社区wiki,而不是删除它。)
interface A {
    name: string;
    age: number;
}
interface B {
    name: string;
    age: number;
}
let a: A = {name: "Joe", age: 27};
let b: B;
b = a;
interface A {
    name: string;
    age: number;
    rank: string;
}
interface B {
    name: string;
    age: number;
}
let a: A = {name: "Joe", age: 27, rank: "Junior Petty Officer"};
let b: B;
b = a;