简体   繁体   English

渲染 Blazor 页面内的反应组件

[英]Render react component inside Blazor page

Is it possible to render a react component inside a Blazor page?是否可以在 Blazor 页面内呈现反应组件? I tried adding script tags to the Blazor page, but Blazor does not allow adding script tags.我尝试将脚本标签添加到 Blazor 页面,但 Blazor 不允许添加脚本标签。

Thanks for the answer.感谢你的回答。

Is it possible to render a react component inside a Blazor page?是否可以在 Blazor 页面内呈现反应组件?

Yes, it's possible.是的,这是可能的。 React is a javascript library and the same way you can add jquery to your blazor app, you can also add React to it, but it have some drawbacks. React 是一个 javascript 库,就像你可以将 jquery 添加到你的 blazor 应用程序中一样,你也可以向它添加 React,但它有一些缺点。

I tried adding script tags to the Blazor page, but Blazor does not allow adding script tags.我尝试将脚本标签添加到 Blazor 页面,但 Blazor 不允许添加脚本标签。

Indeed, you can't add script tag in a .razor file and the place you should add is in the _Host.cshtml file实际上,您不能在.razor文件中添加脚本标签,而您应该添加的位置是在_Host.cshtml文件中

Now you may ask, how to add React to a Blazor app?现在你可能会问,如何将 React 添加到 Blazor 应用程序中? Well.. it's a little complicated.嗯..有点复杂。

What you need to do is configure a webpack to generate all files from React and put then in wwwroot and reference all the scripts and links in the _Host.cshtml file.您需要做的是配置 webpack 以从 React 生成所有文件,然后放入wwwroot并引用_Host.cshtml文件中的所有脚本和链接。

And then you will need to attach to window a function that will render you react component, something like然后你需要附加到window一个 function ,它将呈现你反应组件,就像

// index.js from react
window.renderReactApp = function() {
    ReactDOM.render(<App />, document.getElementById("react-div"))
}

Drawbacks of adding React into a blazor app将 React 添加到 blazor 应用程序的缺点

1. Communicating between React and Blazor. 1. React 和 Blazor 之间的通信。

If you need to pass data (state) between React and Blazor, it will be a big struggle, you will need to create some sort of Flux pattern that will be the bridge between both.如果您需要在 React 和 Blazor 之间传递数据(状态),这将是一场巨大的斗争,您将需要创建某种 Flux 模式作为两者之间的桥梁。 This takes time to do and using it can be confusing.这需要时间来完成,并且使用它可能会令人困惑。

2. Too much effort for something you might be able to do with Blazor 2. 你可能用 Blazor 做的事情太多了

If you are already using Blazor and want to add React, you need to have really strong reasons to do that.如果你已经在使用 Blazor 并且想要添加 React,那么你需要有充分的理由这样做。 Otherwise, you will wast too much time on something that you could have done with less effort in Blazor否则,您将在 Blazor 中花费较少精力就能完成的事情上浪费太多时间

Good reasons to add React to Blazor将 React 添加到 Blazor 的充分理由

There is a lot of js libraries that probably won't be able to be created with Blazor, or if someone do it, probably will be just a wrapper of the js and...有很多js库可能无法用Blazor创建,或者如果有人这样做,可能只是js的包装器......

So far, Blazor doesn't give any way for you to do things that webpack can do, so in some cases where you have a lot of javascript or css, you will want to configure a webpack to minify / do what you need with those files. So far, Blazor doesn't give any way for you to do things that webpack can do, so in some cases where you have a lot of javascript or css, you will want to configure a webpack to minify / do what you need with those文件。 In this case, if you are already configuring the webpack for that, adding react to it, will be no problem.在这种情况下,如果您已经为此配置了 webpack,那么添加 react 就没有问题了。

One example of this is MatBlazor . MatBlazor就是一个例子。 They use a lot of js and css from material design web components, so they use webpack to bundle everything up.他们使用了很多来自material design web组件的js和css,所以他们使用webpack来捆绑一切。 They don't use React, but it's an example of how to use webpack in blazor (something you will need to do to have React in your blazor app)他们不使用 React,但它是如何在 blazor 中使用 webpack 的示例(您需要在 blazor 应用程序中使用 React)

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

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