Search code examples
reactjsreact-reduxreactjs.net

How can I render the React app with redux using the ReactJS.NET?


I have created the Single page application where I have used the React, Redux and React-Router.

I want to render this SPA using ReactJS.NET for improve performance of loading the application.

The SPA is compiled as one Bundle.js using Webpack.

I have an ASP.NET MVC application where I can paste this bundle which will be rendered through the ReactJS.NET. Everything in this SPA will be worked in one ASP.NET view but with the React-router.

How can I do that? I can't find any project where is solved this combination with Redux.


Solution

  • I know this is an old question but if you're still having issues incorporating your React app into React.NET you should try the following template and have a look at how this fellow has done it.

    He uses webpack to first build and compile a server specific set of code then pulls the compiled js into React.NET

    app.UseReact(config =>
    {
        config
            .SetLoadBabel(false)
            .AddScriptWithoutTransform("~/js/react.server.bundle.js");
    });
    

    The webpack config looks like this.

    var path = require('path');
    var webpack = require('webpack');
    var WebpackNotifierPlugin = require('webpack-notifier');
    
    module.exports = {
        entry: {
            server: './React/server.jsx',
            client: './React/client.jsx',
            clientWithRender: './React/clientWithRender.jsx',
        },
        output: { path: __dirname + '/wwwroot/js/', filename: 'react.[name].bundle.js' },
        module: {
            loaders: [
                {
                    test: /.jsx?$/,
                    loader: "babel-loader",
                    exclude: /node_modules/,
                    query: {
                        presets: ['es2015', 'react'],
                        plugins: ['react-html-attrs', 'transform-class-properties', 'transform-decorators-legacy']
                    }
                }
            ]
        },
        plugins: [
            new WebpackNotifierPlugin() 
        ]
    };
    

    And heres the index

    @{
        Layout = null;
    }
    <html>
    <head>
        <title>Hello React</title>
    </head>
    <body>
        <div id="app">
            @{
                if (Environment.GetEnvironmentVariables()["ASPNETCORE_ENVIRONMENT"].ToString() != "Development")
                {
                    @Html.React("ReactComponents.App", new { val1 = "Top text" });
                }
            }
        </div>
    
        @{
            if (Environment.GetEnvironmentVariables()["ASPNETCORE_ENVIRONMENT"].ToString() != "Development")
            {
                <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.4.2/react.min.js"></script>
                <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.4.2/react-dom.min.js"></script>
                <script src="@Url.Content("~/js/react.client.bundle.js")"></script>
                @Html.ReactInitJavaScript()
            }
            else
            {
                <script src="@Url.Content("~/js/react.clientWithRender.bundle.js")"></script>
            }
        }
    </body>
    </html>