简体   繁体   English

在与打字稿反应时初始化空状态的正确方法是什么

[英]What's the correct way to initailise empty states in react with typescript

I'm quite new to react and was wondering what was the correct way to initialise empty state variables in react.我对反应很陌生,想知道在反应中初始化空状态变量的正确方法是什么。 Currently I have a form in typescript which has several fields to fill.目前我有一个打字稿表格,其中有几个字段需要填写。 Most of those fields are dropdowns and i've initialised some static data as an array of objects something like this.大多数这些字段都是下拉菜单,我已经将一些静态数据初始化为类似这样的对象数组。

export const options = [
    {
        id : 0,
        name : "Setting 1"
    },
    {
        id : 1,
        name : "Setting 2"
    },
    {
        id : 2,
        name : "Setting 3"
    }

];

So I'm able to display these options it in the dropdown of the form and everything is working as expected.所以我可以在表单的下拉菜单中显示这些选项,一切都按预期工作。 Problem comes when i try and initialise the state that I'm using to check up on the updates on this field.当我尝试初始化我用来检查该字段更新的状态时,问题就出现了。

const [myForm, setMyForm] = React.useState({
        dropdown : options[0],
        dropdown1 : options1[0]
    });

So for me in the myForm object that I've cerated to keep track of the state, I get the first option by default as selected as I've specified.因此,对于我已创建以跟踪状态的myForm对象中的我来说,默认情况下我会按照我指定的方式选择第一个选项。 I cannot initialise it to null as typescript requires a type for any declaration.我无法将其初始化为null因为打字稿需要任何声明的类型。 What's the best practice to initially have the value as an empty object, and set it later when the user checks the option from the dropdown.最初将该值作为一个空对象并稍后在用户从下拉列表中检查选项时设置它的最佳实践是什么。 Because if i set it to an empty object like {} in the options i cannot access the id and the name fields in the code.因为如果我在options中将它设置为像{}这样的空对象,我将无法访问代码中的idname字段。 Can anyone tell me what's the correct way to do it谁能告诉我正确的做法是什么

useState can take a type. useState可以采用一种类型。 This is good for when Typescript can't infer the type from the type of the initial value.当 Typescript 无法从初始值的类型推断类型时,这很有用。 This is often the case with any null default value.任何null默认值通常都是这种情况。 You need to tell typescript what the value could be when it's not null .您需要告诉打字稿当它不是null时可能是什么值。

For example:例如:

const [name, setName] = useState<string | null>(null)

// name is of type: string | null
if (name != null) console.log(name.toUpperCase())

I don't entirely follow your code here, but I think you may want to set your state's type like this, with a value as an optional.我并不完全遵循您的代码,但我认为您可能希望像这样设置您的状态类型,并将值作为可选值。 This means it's allowed to be missing or undefined.这意味着它可以丢失或未定义。

interface State {
  dropdown: FormField
  dropdown1: FormField
}

interface FormField {
  id: number
  name: string
  value?: string
}

And then use that type:然后使用该类型:

const [myForm, setMyForm] = React.useState<State>({
  dropdown : options[0],
  dropdown1 : options1[0]
});

myForm.dropdown.name // allowed, and type is: string | undefined

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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