简体   繁体   English

React 中动态呈现的组件不正确的大小写错误

[英]Incorrect casing error with dynamically rendered component in React

So, i'd like to spare time later and want to do a dynamically generated page.所以,以后有空,想做一个动态生成的页面。 For that reason i want to read component data from an object, like this:出于这个原因,我想从 object 中读取组件数据,如下所示:

  layout: {
    toolbar: {
      components: [
        {
          type: "Testcomp",
          theme: "default",
          data: "div1"
        },
        {
          type: "Testcomp",
          theme: "pro",
          data: "div2"
        },
      ]}}

The component would be dynamically imported, enabled/activated and besides that this is the jsx code supposed to render components dynamically:该组件将被动态导入、启用/激活,除此之外,这是应该动态呈现组件的 jsx 代码:

render() {
    const toolbarComponents = userSession.layout.toolbar.components.map(Component => (
      <Component.type theme={Component.theme} data={Component.data} key={this.getKey()} />
    ));

    return (
      <div>
        <div className="toolbar">
          toolbar
          {toolbarComponents}
        </div>
    . . .
      </div>
    );
  }

However i get the following warning in Chrome's devtool, also the component is not displayed:但是我在 Chrome 的 devtool 中收到以下警告,而且该组件未显示:

Warning: is using incorrect casing.警告:使用了不正确的外壳。 Use PascalCase for React components, or lowercase for HTML elements.对 React 组件使用 PascalCase,或对 HTML 元素使用小写。

Warning: The tag is unrecognized in this browser.警告:标签在此浏览器中无法识别。 If you meant to render a React component, start its name with an uppercase letter.如果你打算渲染一个 React 组件,那么它的名字以大写字母开头。

What's wrong with my code?我的代码有什么问题?

You are getting those errors because you are not referencing the component itself here, instead using a string as name.您收到这些错误是因为您没有在此处引用组件本身,而是使用字符串作为名称。 So, maybe you need to think another way to create the components dynamically.因此,也许您需要考虑另一种动态创建组件的方法。 Like starting with a base component and only give some props and data to it.就像从一个基础组件开始,只给它一些道具和数据。

// Define above main component or elsewhere then import.
const MyComponent = ( props ) => <div>{props.data}</div>

// Main component
render() {
  const toolbarComponents = userSession.layout.toolbar.components.map(
    component => <MyComponent theme={component.theme} data={component.data} />
  );

  return <div className="App">{toolbarComponents}</div>;
}

Here we are not using a type key anymore.这里我们不再使用类型键。 If you want to use different components like that, you can create every base component and then use as its name with type key but not with string, directly referencing the component.如果你想像这样使用不同的组件,你可以创建每个基本组件,然后使用类型键而不是字符串作为它的名称,直接引用组件。

I tried using the same method as you, wherein I was using a string to reference a React component by name.我尝试使用与您相同的方法,其中我使用字符串按名称引用 React 组件。 However, it doesn't appear designed to be used outside of standard tags like div .但是,它似乎并未设计为在标准标签(如div )之外使用。

Instead, what worked for me is to import the component I wanted to show and then set that in the state.相反,对我有用的是导入我想显示的组件,然后将其设置在 state 中。

import MyComponent from 'mycomponent';

class Parent extends React.Component {
  constructor() {
    super();
    this.state = {
      selectedComponent: null
    };
  }

  render() {
    return (
      <div className="Parent">
        <h2>Parent Component</h2>
        <button onClick={() => this.setState({ selectedComponent: MyComponent })}>Show my component</button>
          {this.state.selectedComponent !== null && React.createElement(this.state.selectedComponent, null, null)}
        </div>
    );
  }
}

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

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