4

I have been trying to get the Flux Todo List Tutorial running in an MVC project using ReactJS.Net.

I am using Gulp and Browserify to bundle files and then making a call to render the React component in my view. Relevant part of gulpfile.js:

gulp.task('build', function(){
  browserify({
    entries: [path.ENTRY_POINT],
    transform: [reactify]
  })
    .bundle()
    .pipe(source(path.NON_MINIFIED_OUT))
    .pipe(gulp.dest(path.DEST_BUILD));
});

I have changed the App.js file to define my component globally:

global.React = require('react');

global.TodoApp = require('./components/TodoApp.react');

And am trying to use in my view like so:

<div id="app">
    @Html.React("TodoApp", new {})
</div>

<script src="http://fb.me/react-0.13.3.min.js"></script>
@Scripts.Render("~/bundles/reactApp")
@Html.ReactInitJavaScript()

But it throws the following error at run time:

Could not find a component named 'TodoApp'. Did you forget to add it to App_Start\ReactConfig.cs?

I have added the output from Browserify to my ReactConfig.cs:

public static void Configure()
{
    ReactSiteConfiguration.Configuration
        .AddScript("~/dist/build.js"); // Have also tried .AddScriptWithoutTransform
}

But it doesn't seem to help.

It works fine if I just render on client side:

<div id="app">
</div>

@Scripts.Render("~/bundles/reactApp")
<script>
    React.render(
        React.createElement(TodoApp, null),
        document.getElementById('app')
    );
</script>
5
  • 1
    Can you post the full code of what you've done so far so I can take a look? Commented Jun 30, 2015 at 17:06
  • @DanielLoNigro I've added the full source code to a GitHub repo: github.com/RagtimeWilly/FluxMvcExample Commented Jul 1, 2015 at 0:31
  • @RagtimeWilly Would love to see if you've been able to get this up and running? I'm trying to implement flux with .net and react. stackoverflow.com/questions/31599992/… Commented Jul 24, 2015 at 0:16
  • @nanonerd I have a site up and running using the flux pattern as the view in an ASP.Net MVC site. I still haven't managed to get the server side rendering working though. There's a link to a github project showing the basic idea in my above comment. Let me know if you have questions. Commented Jul 25, 2015 at 8:17
  • @nanonerd You can use react-aspnet-boilerplate to get server-side rendering with flux/redux. github.com/pauldotknopf/react-aspnet-boilerplate Commented Mar 27, 2016 at 12:17

2 Answers 2

1

Check out this blog:

http://janekk.github.io/tech/2014/07/25/aspnet-mvc-reactjs-browserify.html

and a good example here for ReactJs.Net, Gulp and Browserify:

https://github.com/Janekk/ReactDotNetBrowserify

Hope it helps :)

Sign up to request clarification or add additional context in comments.

Comments

0

Browserify does not expose your bundled objects by default so the TodoApp object does not exist. You will need to expose/export TodoApp via browserify require and then require TodoApp before using it in the browser.

The server needs the components exposed in the global space as well. I ended up using something similar to the following:

gulp.task("bundle", function () {
    var exposure = new Readable();
    var b = browserify({
        transform: babelify
    });

    exposure.push('var React = require("react");\n');

    b.require(paths.components + "/Component1.jsx", { expose: 'Component1' });
    exposure.push('var Component1 = require("Component1");\n');

    b.require(paths.components + "/Component2.jsx", { expose: 'Component2' });
    exposure.push('var Component2 = require("Component2");\n');

    exposure.push(null);

    b.require("react");
    new StreamQueue()
        .queue(b.bundle()).queue(exposure)
        .pipe(source("bundle.js"))
        .pipe(gulp.dest(paths.componentJsDest));

});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.