简体   繁体   中英

React Component not rendering in ASP.NET MVC app

I currently have 1 react component rendering in my application, but I'm unable to inject any other component within the app. I don't get any error in the console when I inject my second component into the html file. At this point, i'm not sure what else I'm really missing? Any and all suggestions are welcome! (NOTE: I included a reference to react in my BundleConfig.cs file)

Here is my .cshtml file where I am calling the component:

Administration.cshtml

@model List<object>
@using ms.jts.courtspace.web001Web.Models  
@{

ViewBag.Title = "Administration";
}

<h2>Administration</h2>
<div id="admin" 
     data-userType="@Html.Raw(Newtonsoft.Json.JsonConvert.SerializeObject(M‌​odel));">
</div>

@section Scripts{
   <script src="@Url.Content("~/Scripts/moment.min.js")"></script>
   @Scripts.Render("~/bundles/react")
   @Scripts.Render("~/bundles/bootstrap")
   @Scripts.Render("~/bundles/spcontext")
   <script type="text/jsx" src="@Url.Content("../../App/admin.js")"></script>
}


Here is my react component where I'm also using primereact.

admin.js

import  React, { Component } from "react";
import ReactDOM from "react-dom";
import { TabView, TabPanel } from 'primereact/components/tabview/TabView';

class TabViewDemo extends React.Component {

    render() {
        return (
            <TabView>
                <TabPanel header="Godfather I" leftIcon="fa-calendar">
                    The story begins as Don Vito Corleone, the head of a New York Mafia family, oversees his daughters wedding.
        His beloved son Michael has just come home from the war, but does not intend to become part of his fathers business.
        Through Michaels life the nature of the family business becomes clear. The business of the family is just like the head
        of the family, kind and benevolent to those who give respect,
        but given to ruthless violence whenever anything stands against the good of the family.
    </TabPanel>
                <TabPanel header="Godfather II" rightIcon="fa-print">
                    Francis Ford Coppolas legendary continuation and sequel to his landmark 1972 film, The_Godfather parallels the young
        Vito Corleone's rise with his son Michael's spiritual fall, deepening The_Godfathers depiction of the dark side of
        the American dream. In the early 1900s, the child Vito flees his Sicilian village for America after the local Mafia kills his family.
        Vito struggles to make a living, legally or illegally, for his wife and growing brood in Little Italy, killing the local Black Hand
        Fanucci after he demands his customary cut of the tyro's business. With Fanucci gone, Vito's communal stature grows.
    </TabPanel>
                <TabPanel header="Godfather III" leftIcon="fa-bell-o" rightIcon="fa-bookmark-o">
                    After a break of more than 15 years, director Francis Ford Coppola and writer Mario Puzo returned to the well for this
        third and final story of the fictional Corleone crime family. Two decades have passed, and crime kingpin Michael Corleone,
        now divorced from his wife Kay has nearly succeeded in keeping his promise that his family would one day be completely legitimate.
    </TabPanel>
            </TabView>
        )
    }
}

ReactDOM.render(<TabViewDemo />, document.getElementById("admin"));

package.json

{
  "version": "1.0.0",
  "name": "jts.court.space",
  "private": true,
  "devDependencies": {
    "webpack": "3.10.0",
    "babel-cli": "^6.26.0",
    "babel-preset-env": "^1.6.1",
    "babel-preset-react": "6.24.1",
    "babel-loader": "7.1.2",
    "babel-core": "^6.26.0"
  },
  "dependencies": {
    "axios": "0.17.1",
    "primereact": "^1.3.0",
    "pusher-js": "4.2.1",
    "react": "16.2.0",
    "react-dom": "16.2.0",
    "react-router-dom": "^4.2.2",
    "react-addons-css-transition-group": "^15.6.2",
    "font-awesome": "^4.7.0",
    "classnames": "^2.2.5",
    "react-burger-menu": "^2.1.11"
  },
  "scripts": {
    "build": "webpack --config webpack.config.js"
  },
  "-vs-binding": {
    "AfterBuild": [
      "build"
    ]
  }
}

webpack.config.js

"use strict";

module.exports = {
    entry: {
        app: "./App/app.js",
        admin: "./App/admin.js"},
  output: {
    filename: "./Scripts/bundle.js"
  },
  module: {
    loaders: [
        {
            test: /\.js$/,
            loader: "babel-loader",
            exclude: /node_modules/,
            query: {
                presets: ["env", "react"]
            }
        },        
    ]
  }
};

The answer to my solution was to essentially create another entry point in my webpack.config.js file. Then in the outputs I had to modify the filename to generate two separate bundle.js files. This is what my updated wepback.config.js file looks like now:

"use strict";

module.exports = {
    entry: {
        app: "./App/app.js",
        admin: "./App/admin.js"
    },
  output: {
    filename: "./Scripts/[name]-bundle.js"
  },
  module: {
    loaders: [
        {
            test: /\.js$/,
            loader: "babel-loader",
            exclude: /node_modules/,
            query: {
                presets: ["env", "react"]
            }
        },        
    ]
  }
};

Lastly, I had to add those two new bundle.js files in BundleConfig.cs like so:

bundles.Add(new ScriptBundle("~/bundles/react").Include(
                "~/Scripts/app-bundle.js",
                "~/Scripts/admin-bundle.js"));

I'm new to React, but hopefully this helps someone out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM