简体   繁体   English

如何在 typescript + React 中为 Object 类型的可选嵌套 props 提供默认值?

[英]How do I provide default values to optional nested props of type Object in typescript + React?

I'm using Typescript and React.我正在使用 Typescript 和 React。 I typically assign default props while destructuring them like so:我通常在解构它们时分配默认道具,如下所示:

import * as React from "react"

type ComponentProps = {
  a: string;
  b?: number;
}

const Component: React.FC<ComponentProps> = ({
  a,
  b = 2
}) => {

  //...

}

But if a prop is an object, or even a nested object, is there a way to assign default values to some properties while keeping some as required?但是如果一个 prop 是一个对象,甚至是一个嵌套的对象,有没有办法为某些属性分配默认值,同时根据需要保留一些属性?

For example:例如:

import * as React from "react"

type ComponentProps = {
  a: {
    b: number;
    c?: string;
  };
  d?: {
    e?: number;
    f?: string;
  };
  g?: {
    h?: number;
    f: string;
  };
}

const Component: React.FC<ComponentProps> = ({

  /* I'm not sure what to do here...*/

}) => {
  //...
}

I guess I'm actually asking three questions.我想我实际上是在问三个问题。

  1. While destructuring, can I assign a default value to the optional property of a required object, such as ac ?在解构时,我可以为所需对象的可选属性(例如 ac )分配默认值吗?
  2. While destructuring, if I assign default values to de and df, and when used only de is provided, will df still be retained?解构时,如果给de和df赋默认值,使用时只提供de,df还会保留吗?
  3. Can you have a required property of an optional object, such as gf ?您是否可以拥有可选对象的必需属性,例如 gf ? I think in my mind it's like saying, "This prop is optional, but if you provide it you must provide gf ".我认为在我看来这就像在说,“这个道具是可选的,但如果你提供它,你必须提供 gf”。

It's possible , but nested destructuring can be difficult to read.这是可能的,但嵌套解构可能难以阅读。 I'd recommend avoiding the pattern when possible.我建议尽可能避免这种模式。

While destructuring, can I assign a default value to the optional property of a required object, such as ac ?在解构时,我可以为所需对象的可选属性(例如 ac )分配默认值吗?

If you had to, for a default value for c :如果必须,对于c的默认值:

const Component: React.FC<ComponentProps> = ({
  a: {
    b,
    c = 'foo',
  }
  // ...
}) => {
  //...
}

While destructuring, if I assign default values to de and df, and when used only de is provided, will df still be retained?解构时,如果给de和df赋默认值,使用时只提供de,df还会保留吗?

Yes, if you assign a default object to the whole d :是的,如果您为整个d分配一个默认对象:

const Component = ({
  d: {
    e = 5,
    f = 'foo',
  } = {},
}: ComponentProps) => {
  //...
}

Can you have a required property of an optional object, such as gf您是否可以拥有可选对象的必需属性,例如 gf

Yes, that's just fine.是的,那很好。

暂无
暂无

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

相关问题 你如何为 React 中的嵌套形状提供默认道具? - How do you provide default props for nested shape in React? 如何在React TypeScript组件上设置道具的默认值? - How can I set default values for props on a React TypeScript component? 如何在带有 Typescript 的 React 组件中使用默认道具? - How do I use default props in a React component with Typescript? 如何使用 TypeScript 为无状态的功能性 React 组件指定(可选)默认道具? - How to specify (optional) default props with TypeScript for stateless, functional React components? TypeScript + React:如何键入一个“rest”道具,将道具的 rest 收集到 object 中,然后将它们传播到某个元素? - TypeScript + React: How do I type a `rest` props that collects the rest of the props into an object and later spread them to some element? React TypeScript 道具的默认类型 - React TypeScript default Type for Props 如何为 object 类型设置 React 默认道具 - How to set React default props for object type 使用React + TypeScript,当使用启用了strictNullChecks的默认值的可选道具时,如何避免类型转换? - With React + TypeScript, how do I avoid typecasting when using optional props with defaults with strictNullChecks enabled? 如何访问 React props 中的嵌套对象属性 - How do I access nested object properties within React props React-Typescript:如何输入默认道具? - React-Typescript: How do I type a default prop?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM