简体   繁体   English

当 React 组件在 Typescript 中具有强制道具时,将其作为道具注入

[英]Inject a React component as a prop when it has mandatory props in Typescript

I'm building a React TreeView component with Typescript .我正在使用Typescript构建一个React TreeView组件。 I would like the flexibility to be able to inject a template TreeNode component that renders each node.我希望能够灵活地注入呈现每个节点的模板TreeNode组件。

How can I type my TreeNode 's props so I can inject TreeNode into TreeView as a prop without typescript complaining that I'm not providing my TreeNode s props yet, even though they will be down the road when TreeView builds the hierarchy and feeds each TreeNode with appropriate props .我如何输入我的TreeNode的道具,以便我可以将TreeNode作为道具注入到TreeView ,而无需typescript抱怨我还没有提供我的TreeNode的道具,即使当TreeView构建层次结构并提供每个道具时它们将在路上带有适当props TreeNode

I tried to strip down my code to the minimum:我试图将我的代码精简到最低限度:

interface TreeNodeProps {
    id: string
    title: string
}

const TreeNode: React.SFC<TreeNodeProps > = (props) => {
  return (
    <div key={props.id}>
        <div>{props.title}</div>
        <div>{props.children}</div>
    </div>
  )
}

const TreeView: React.SFC<{treeData: any, TreeNode: JSX.Element}> = (props) => {
  // here's the logic where each TreeNode's props will be fed with graph's props.
    const buildTreeRecursively = (treeData) =>
        treeData.map((node) => {                
            if (node.children.length > 0) {
                return (
                    <TreeNode id={node.id} title={node.title}>
                        {buildTreeRecursively(node.children)}
                    </TreeNode>
                )
            }
            return <TreeNode id={node.id} title={node.title} />
        })

  return (
        <div className={styles.treeView}>
            {buildTreeRecursively(props.treeData)}
        </div>
  )
}

This seems to work:这似乎有效:

const buildTreeRecursively = (treeData: React.PropsWithChildren<TreeNodeProps>[]) =>
    treeData.map((node) => {
        if (node.children) {
            return (
                <TreeNode id={node.id} title={node.title}>
                    {buildTreeRecursively(node.children as React.PropsWithChildren<TreeNodeProps>[])}
                </TreeNode>
            )
        }
        return <TreeNode id={node.id} title={node.title} />
    })

If I leave the if (node.children.length > 0) , I get the compile error如果我离开if (node.children.length > 0) ,我会收到编译错误

Object is possibly 'null' or 'undefined'.对象可能是“空”或“未定义”。

and since the type of node.children is a superset of React.PropsWithChildren<TreeNodeProps>[] , we have to cast it since we know we're only using that subset of the type.并且因为node.children的类型是React.PropsWithChildren<TreeNodeProps>[]的超集,我们必须转换它,因为我们知道我们只使用该类型的子集。

I got the idea for treeData 's type by looking at the type hint for props from通过查看props的类型提示,我得到了treeData类型的想法

const TreeNode: React.SFC<TreeNodeProps> = (props) => {
  return (
    <div key={props.id}>
        <div>{props.title}</div>
        <div>{props.children}</div>
    </div>
  )
}

which was React.PropsWithChildren<TreeNodeProps> .这是React.PropsWithChildren<TreeNodeProps>

暂无
暂无

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

相关问题 在 React Typescript 中使用 props 作为 prop 传递组件 - Pass down component with props as prop in React Typescript Typescript 在 React 功能组件(React.FC<props> ) 错误的道具是作为道具发送的吗?</props> - Typescript isn't complaining when a React Functional Component (React.FC<Props>) with wrong props is send as a prop? 将子项注入到 TypeScript 中由 props 传递的反应组件中 - Inject children into react component passed by props in TypeScript 在将组件作为道具传递时,如何在 TypeScript(在 React 中)中强制传递道具? - How do I enforce passthrough props in TypeScript (in React) when passing a component as prop as well? Typescript React:使用传递给同一组件的另一个道具有条件地将道具标记为可选或强制性 - Typescript React: Conditionally mark prop to optional or mandatory using another prop passed to same component 如何为React props定义TypeScript类型,只有在将prop A传递给组件时,才会接受prop B? - How do I define a TypeScript type for React props where prop B is only accepted if prop A is passed to a component? React和TypeScript-组件<Props & {}> - React and TypeScript - Component<Props & {}> 如何注入React Component道具? - How to inject into React Component props? 如何确保 React 组件在 TypeScript 中具有“关键”属性 - How to ensure a React component has 'key' prop in TypeScript 是否必须传递道具来反应功能组件? - Is it mandatory to pass props to react functional component?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM