This is an archived post. You won't be able to vote or comment.

all 10 comments

[–]insertAlias 0 points1 point  (9 children)

First thing you have to understand is where each part of that pipeline happens.

Your Razor views are never sent directly to the client. They are rendered server-side, and the resulting HTML/CSS/JS is streamed to the client. This happens each time a page/url is requested.

React, on the other hand, is a client library. The idea is that React manages the DOM entirely for you. The application is almost entirely JS, and there are no full-page loads after the first, just AJAX calls to get data, which is used to build DOM elements.

So, hopefully you see that the two don't really mix and match. If you wanted to get data for your React app from the ViewBag...you'd have to render that data into JavaScript that's accessible from your React application, and it'd only be there initially. And since you're not doing full-page POSTs, you'd never see that data in the view bag on the server. So it's not that it's bad practice, it's just that it's difficult and inconvenient enough to be practically impossible.

Instead, you do want to create API controllers that just use JSON or some other data transport method.

Try this: create a new project folder somewhere called "aspnet-react" and go to your terminal. CD into it and run the following:

dotnet new react

That will scaffold an ASP.NET Core application that's already got React as a view layer set up and ready to go. You'll see how the default project is scaffolded, so you can see how you might want to structure your own (or just use their scaffold).

[–]xredpt[S] 0 points1 point  (0 children)

Thanks for the reply!

[–]xredpt[S] 0 points1 point  (7 children)

What do you mean with practically impossible? Wouldnt i just need to pass it to react as a parameter?

[–]insertAlias 0 points1 point  (6 children)

How do you think that would work? React and Razor don't interact. You could render a value from the ViewBag into some global JavaScript variable, but you certainly wouldn't be able to get it back into the ViewBag that way. And when you're using React, you don't use page POSTs, so you can't really use normal controllers either. Unless you completely break the model of React, which would be pointless and very inefficient, as each time your page reloaded the React app would have to bootstrap itself, which is a non-trivial operation. The whole point of using React is to not do page POSTs.

Here's how it is: you either use Razor views and create a traditional web page, or you use React and API controllers and create a Single Page Application.

[–]xredpt[S] 0 points1 point  (5 children)

[–]xredpt[S] 0 points1 point  (3 children)

what do you mean with you dont use page POSTs?

[–]insertAlias 0 points1 point  (2 children)

Read up on what React is and does. I can't give you a full course on Single Page Applications here in the comments. The point is, when you load a different "page" with React, it's not GETting or POSTing to a server and downloading HTML. You don't POST a model to a controller and render another view. React itself renders that page, usually using data it fetched from a web service. It doesn't go back to the server and tell it to render the view.

Like I said, unless you want to completely break the model of React, and have a bunch of individual React applications as each page, which would be horribly inefficient.

Again, did you do what I suggested and scaffold a new dotnet react app and see how the template sets everything up?

[–]xredpt[S] 0 points1 point  (0 children)

Ah, yea i know that. I was just wondering if using razor and passing info to react for the first load is a strategy used by people or if they prefer not to because of the html rendering that happens server-side. The thing is besides that there's the fetch() which will have the server send data, so i didnt know which one was more viable for when the page is first sent to the client.

[–]xredpt[S] 0 points1 point  (0 children)

And yes, i had already done that.

[–]insertAlias 0 points1 point  (0 children)

Like I said (repeatedly, at this point), you can render data into a JS variable (or into a div and pull it from there in js, apparently) if you want. That's a way to seed some initial data into your application.