简体   繁体   English

将 react.js 中已渲染元素的 innerHTML 设置为组件

[英]Setting the innerHTML of an already rendered elment in react.js to be a component

Am working with the ionic ui library in react.js and here is my code我正在使用 react.js 中的 ionic ui 库,这是我的代码

 <IonTabBar slot="bottom">
      {page.tabs.map(tab => {
        const Icon = Icons[`${tab.icon}`];

        return (
          <IonTabButton>
            <Icon
              style={{
                fontSize: "20px"
              }}
            />
            <IonLabel
              style={{
                fontSize: "14px"
              }}
            >
              {" "}
              {tab.text}{" "}
            </IonLabel>
          </IonTabButton>
        );
      })}
    </IonTabBar>
  </IonTabs>

And when renders on the browser it has this当在浏览器上呈现时,它有这个

 <div>
  <div class="tabs-inner"> </div>
  <ion-tab-bar>
    <ion-tab-button>
      <svg>
       .....
      </svg>
      <ion-label>
        user
      </ion-label>
    </ion-tab-button>
  </ion-tab-bar>
</div>

Now i would like to inject a component into the class "tabs-inner" so my soultion was to give the div a ref and find it using现在我想将一个组件注入到“tabs-inner”类中,所以我的灵魂是给 div 一个引用并使用它找到它

 const tab = tabsRef.current.getElementsByClassName('tabs-inner')[0]

Now I would like to inject a component into the div with the tabs-inner class say现在我想将一个组件注入到带有tabs-inner类的 div 中说

tab.innerHTML = 'am here'

That works but if I try那行得通,但如果我尝试

tab.innerHTML = <Mycomponent/>

it doesn't, am trying to set the innerHTML to be a tag so I can inject components into it.它没有,我试图将 innerHTML 设置为一个标签,以便我可以将组件注入其中。

Any ideas?有任何想法吗?

You can try to put your component to be rendered in the state of the Component and render it accordingly.您可以尝试将要渲染的组件置于 Component 的状态并进行相应的渲染。 In case if you do not want to render anything at a particular time, you can set that state key to null.如果您不想在特定时间渲染任何内容,您可以将该状态键设置为 null。

constrcutor(props) {
    super(props);
    this.state = {
        componentToRender: null
    };
}

componentDidMount() {
    setTimeout(() => {
        this.setState({
            componentToRender: <b>Hello World</b>
        });
    }, 1000);
}

render() {
    return (
        <div className="">
            { this.state.componentToRender }
        </div>
    );
}

In the above example, I am just setting the HTML after 1 second but you can set the data whenever you want it to be.在上面的示例中,我只是在 1 秒后设置 HTML,但您可以随时设置数据。

So after doing a bit of digging, I found a not so wise way to do it using the react-dom import hydrate因此,在进行了一些挖掘之后,我发现了一种使用react-dom导入hydrate的不太明智的方法

import { hydrate } from "react-dom";
.........
const tab = tabsRef.current.getElementsByClassName("tabs-inner")[0];
hydrate(
  <Canvas/>,
  tab
);

This works for most cases and would have worked cool for me but am using redux in my canvas container and it is generating other errors.这适用于大多数情况,对我来说很酷,但我在我的画布容器中使用了 redux 并且它产生了其他错误。

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

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