7

I wanted to tryout React and started following the tutorial here Took the Internet template, added the required script references in _Layout.cshtml and placed the following script in About.cshtml.

This code with the '@' char is giving trouble:

<script type="text/jsx">
  /**
   * @jsx React.DOM
   */
  // The above declaration must remain intact at the top of the script.
  // Your code here
</script>

MVC says:

The name 'jsx' does not exist in the current context

Please provide some pointers

Regards.

4
  • Have you tried putting the script references in About.cshtml too? Commented Apr 2, 2014 at 8:58
  • Yup, same message there too. Commented Apr 2, 2014 at 9:02
  • Do you have enclosed this tag after reference the React script ?, Better to see and tried this demo github.com/facebook/react/tree/master/examples Commented Apr 2, 2014 at 9:21
  • Gupta, those are all for Js MVC frameworks Commented Apr 2, 2014 at 9:35

2 Answers 2

10

I've just released ReactJS.NET which lets you easily compile JSX to JavaScript. With ReactJS.NET, you can put your code into a .jsx file (say /Scripts/HelloWorld.jsx) and then reference it via a script tag:

<script src="@Url.Content("~/Scripts/HelloWorld.jsx")"></script>

Additionally, ASP.NET Bundling and Minification and Cassette are both supported.


Original answer (pre 4th April 2014):

You need to escape the @ by writing it twice:

<script type="text/jsx">
  /**
   * @@jsx React.DOM
   */
  // The above declaration must remain intact at the top of the script.
  // Your code here
</script>

I'm currently working on some stuff to make it easier to use React from ASP.NET, which should be coming out very soon. This includes server-side compilation of JSX (both on the fly and via ASP.NET minification and combination) and optional server-side rendering of React components. Stay tuned!

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

1 Comment

Hey, it looks like I can't use the React or ReactInitJavaScript HTML helpers in my Razor views... I've tried following several StackOverflow and other posts and finally ended up posting my own. Can you take a gander and see if you can give your two cents? I was able to follow the CommentBox tutorial RIGHT up to the last optimization challenge. :)
0

Take a look my project, a starter template for using React in .NET.

https://github.com/pauldotknopf/react-dot-net

This project solves many issues that are present in the other projects (NodeServices and ReactJS.NET), including truely isomorphic applications with matching server/client routing.

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.