简体   繁体   English

从ASP.NET初始化React组件

[英]Initializing react component from asp.net

Hopefully this is a slam-dunk for someone out there...my essential problem is this: I've built up a very nice set of react components which i can render in my asp.net 4.5 mvc 6 application using react.js, flux, gulp, and browserify. 希望这对外面的人来说是灌篮高手……我的基本问题是:我建立了一套非常不错的react组件,可以使用react.js在我的asp.net 4.5 mvc 6应用程序中进行渲染,流量,吞咽和浏览器化。

as long as i have it structured so that the react components have all the data they need everything is perfect. 只要我具有结构化的结构,以便反应组件拥有他们需要的所有数据,那么它们就是完美的。 My issue now is that I would like to have an MVC view include the react stuff, and inject run-time properties into the top-level component as it is created. 现在的问题是,我希望MVC视图中包含react内容,并在创建时将运行时属性注入顶级组件中。 Since I'm brpowserify-ing all of my react code into a bundle, i just include the one script tag in my view: 由于我将所有的反应代码都打包了,所以我只在视图中包含一个脚本标签:

<script src="/js/modules/AuthContainer.jsx"></script>

But whereas I would normally use JSX syntax to instantiate my component with props like this: 但是,尽管我通常会使用JSX语法通过以下示例来实例化我的组件:

...the view in ASP.NET never gets translated to pure JS, so that fails. ... ASP.NET中的视图永远不会转换为纯JS,因此失败。

I've also tried: 我也尝试过:

  ReactDOM.render
  (
     React.createElement(AuthContainer, { successPath: '/home' }),
     document.getElementById('reactRoot')
  );

...from inside a script block in my view but i get: ...从我认为的脚本块内部,但我得到:

Uncaught ReferenceError: AuthContainer is not defined

But i'm sure i'm exposing 'AuthContainer' via the browserify-ed bundle, so i don't understand why it's unable to resolve that component. 但是我确定我是通过browserify-ed捆绑包公开“ AuthContainer”的,所以我不明白为什么它无法解析该组件。

I know there's a React.NET way to do this, but i can't get that server-side rendering to work with my components because I'm using jQuery to fetch data in componentDidMount and the server-side rendering is choking looking for $() jQuery stuff. 我知道有一种React.NET的方法可以做到这一点,但是我无法让该服务器端渲染与我的组件一起使用,因为我正在使用jQuery在componentDidMount中获取数据,而服务器端渲染正在寻找$() jQuery的东西。

I'd love to get the server side rendering going but right now i just need it to do work, one way of the other. 我很想让服务器端渲染正常进行,但现在我只需要它即可工作,一种是另一种方式。 Can someone provide a simple code snippet or gist of how to instantiate a React component from inside a cshtml file with run-time props? 有人可以提供一个简单的代码片段或要点,说明如何使用运行时道具从cshtml文件内部实例化React组件吗?

One easy solution is this, just put your server side properties with Javascript in a global: 一个简单的解决方案是,只需将带有Javascript的服务器端属性放在全局变量中:

index.cshtml

<script>
var __config__ = {
  base: "@MyBackEdnVariable",
  initialCount: "@Count",
  user: {
    id: @user.id,
    name: @user.name,
  }
};
</script>
<script src="/js/modules/AuthContainer.jsx"></script>

And with React use that global variable: 并与React使用该全局变量:

AuthContainer.js

class AuthContainer extends React.Component {
  render() {
    return (
      <div>{this.props.user.name}</div>
    );
  }
}

AuthContainer.defaultProps = {
  initialCount: __config__.initialCount,
  user: __config__.user
};

For posterity: 对于后代:

ReactDOM.render
(
    React.createElement
    (
        MyComponent, 
        {
            prop1: @numericValue,
            prop2: '@textValue',
        }
    ), 
    document.getElementById('reactRoot')
);

the magic was the jsx-alternative syntax, which i was aware of couldn't get a handle on that day. 魔术是jsx替代语法,我知道那天无法处理。 This allows you to instantiate react using pure JS and therefor just embed inside a simple script tag in your cshtml. 这使您可以使用纯JS实例化反应,因此只需将其嵌入cshtml中的简单脚本标签中即可。

hth. 心连心。

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

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