2

I have a pretty basic, but probably complicated question.

I am using ReactJS.net to render react components server side. I have a typeahead component which I use multiple times throughout my site. One of the properties it expects is a javascript filter function.

I don't want to have to create a seperate component for each instance of the typeahead, but would like to pass in a javascript function as a property so that I can reuse the component throughout the site.

For example, to render a component on the server I would do the following. The second parameter is the properties argment

@Html.React("Components.WorkSiteTypeahead", new { filterFn = model.SomeFunction })

Now as ReactJS.net expects native c# objects (string, array, list, etc), I don't see any straight forward way to pass in a Javascript function. Any ideas on how would I go about telling my MVC5 view to render my function not as a string? My first instinct is that there might be some sort of Javascript Raw type I am not aware of, but haven't been able to find it.

2
  • 1
    Of course after I posted this I figured it out. Newtonsofts JRAW does the trick filterFn= new JRaw("function() {doStuff())}") Commented Mar 28, 2015 at 17:54
  • You should post your comment as an answer and mark it as accepted. It's okay to accept your own answer :) Commented Mar 29, 2015 at 21:13

1 Answer 1

6

As I suspected, at least one way to go about this is to use Newtonsoft's JRAW Type.

@Html.React("Components.WorkSiteTypeahead", new { filterFn = JRaw("function(){//do stuff } "})
Sign up to request clarification or add additional context in comments.

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.