如何在 Angular 6 应用程序中使用 React 组件

Sri*_*ree 4 reactjs angular angular6

我有一些反应组件,我想在 angular 6 应用程序中重用相同的组件。可以来一些建议我最好的方法来做到这一点。

Est*_*ask 8

可能有第三方助手用于此目的,但它们的行为方式相同,React 组件在需要时被渲染和卸载:

@ViewChild('container') containerRef: ElementRef;

ngOnInit() {
  ReactDOM.render(<ReactComponent/>, this.containerRef.nativeElement);
}

ngOnDestroy() {
  ReactDOM.unmountComponentAtNode(this.containerRef.nativeElement);
}
Run Code Online (Sandbox Code Playgroud)

React 组件可以通过 props 与 Angular 组件交互。

  • 优秀的简单例子。小修正:我认为渲染应该发生在“ngAfterViewInit”回调中,而不是“ngOnInit”中,以确保“@ViewChild”提供的引用可用。 (2认同)