Reputation: 4733
I am getting started with react js, Build my first component i am stuck on this error
"Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined."
The editor i am using is visual studio 2017 for this.
code for webpack.config.js is
module.exports = {
context: __dirname,
entry: "./app.js",
output: {
path: __dirname + "/dist",
filename: "bundle.js"
},
watch: true,
module: {
rules: [{
test: /\.js$/,
exclude: /node_modules/,
use:
{
loader: 'babel-loader',
options: {
presets: ['babel-preset-env', 'babel-preset-react', ]
}
}
}
]
}
}
app.js
import React from 'react';
import ReactDOM from 'react-dom';
ReactDOM.render(
<Button />,
document.getElementById('root')
);
class Button extends React.Component {
render() {
return (<button>42</button>);
}
}
the page that i am rendering is index.cshtml
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Index test</h2>
<div id="root"></div>
@section scripts{
<script src="~/Scripts/React/dist/bundle.js"></script>
}
After i make a change in app.js,next step is to got to cmd prompt and do npm run webpack.
Thanks for the help
Upvotes: 0
Views: 2349
Reputation: 1636
You are using the component before declaring it. This doesn't work because the javascript classes don't get hoisted. This should work
import React from 'react';
import ReactDOM from 'react-dom';
class Button extends React.Component {
render() {
return (<button>42</button>);
}
}
ReactDOM.render(
<Button />,
document.getElementById('root')
);
Upvotes: 2