简体   繁体   English

维护一个 React 组件列表,然后渲染到父组件

[英]Maintaining a List of React Components then rendering into parent

I have a hypothetical question about maintaining an array of polymorphic React components.我有一个关于维护多态 React 组件数组的假设问题。 Is it possible/good React practice to maintain an array of components that are descendants of a common component, then render them in the container?是否有可能/良好的 React 实践来维护作为公共组件后代的组件数组,然后在容器中呈现它们? For instance:例如:

import * as React from 'react';

import GenericBlock from './GenericBlock';
import { BlockTypeA, BlockTypeB, BlockTypeC } from './MyBlocks';

export default class BlockHolder extends React.Component {
    blocks : GenericBlock[] = [ <BlockTypeA />, <BlockTypeB />, <BlockTypeC /> ];

    render() {
        return (
            <div id="workspace">
                {
                    this.blocks
                }
            </div>);
    };
};

GenericBlock.tsx通用块.tsx

import * as React from 'react';

export default class GenericBlock extends React.Component {
    render() { return (<div></div>); }
}

MyBlocks.tsx: MyBlocks.tsx:

import * as React from 'react';

import GenericBlock from './GenericBlock';

class BlockTypeA extends GenericBlock {
    render() { return (<div></div>); }
};

class BlockTypeB extends GenericBlock {
    render() { return (<div></div>); }
};

class BlockTypeC extends GenericBlock {
    render() { return (<div></div>); }
};

export { BlockTypeA, BlockTypeB, BlockTypeC };

The above code snippet yields the error: Objects are not valid as a React child , but I think it captures the spirit of what I am talking about.上面的代码片段产生了错误: Objects are not valid as a React child ,但我认为它抓住了我所说的精神。 Is there a way to make the above scheme work?有没有办法使上述方案奏效?

I am sure that this question has already been asked and answered, but I can't seem to get the Google Search correct.我确信已经提出并回答了这个问题,但我似乎无法让 Google 搜索正确。

EDIT :编辑

Added Sandbox link: https://codesandbox.io/s/wizardly-wilson-vb7nn添加沙盒链接: https://codesandbox.io/s/wizardly-wilson-vb7nn

Now I am encountering a new error: Type 'Element' is missing the following properties from type 'GenericBlock': render, context, setState, forceUpdate, and 2 more.现在我遇到了一个新错误: Type 'Element' is missing the following properties from type 'GenericBlock': render, context, setState, forceUpdate, and 2 more.

EDIT 2 :编辑 2

Typing the blocks array as JSX.Element removes the error and makes everything work, but that doesn't seem like very good practice as JSX.Element can be any element, whereas the point of typing it as GenericBlock is to ensure that all the elements are descendants of a specific component.blocks数组键入为 JSX.Element 可以消除错误并使一切正常,但这似乎不是很好的做法,因为JSX.Element可以是任何元素,而将其键入为GenericBlock的目的是确保所有元素是特定组件的后代。

in react code, it works perfectly: https://codesandbox.io/s/optimistic-ptolemy-g25ge在反应代码中,它完美地工作: https://codesandbox.io/s/optimistic-ptolemy-g25ge

no errors, no warnings没有错误,没有警告

import * as React from "react";

import { BlockTypeA, BlockTypeB, BlockTypeC } from "./MyBlocks";

export default class BlockHolder extends React.Component {
  blocks = [
    <BlockTypeA key={1} />,
    <BlockTypeB key={2} />,
    <BlockTypeC key={3} />
  ];

  render() {
    return <div id="workspace">{this.blocks}</div>;
  }
}

import * as React from "react";

export default class GenericBlock extends React.Component {
  render() {
    return <div />;
  }
}

import * as React from 'react';

import GenericBlock from './GenericBlock';

class BlockTypeA extends GenericBlock {
    render() { return (<div>A</div>); }
};

class BlockTypeB extends GenericBlock {
    render() { return (<div>B</div>); }
};

class BlockTypeC extends GenericBlock {
    render() { return (<div>C</div>); }
};

export { BlockTypeA, BlockTypeB, BlockTypeC };
"dependencies": {
    "react": "16.12.0",
    "react-dom": "16.12.0",
    "react-scripts": "3.0.1"
  },
  "devDependencies": {
    "typescript": "3.8.3"
  }

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

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