React.js - "Invariant Violation: Element type is invalid: expected a string" Simple Button
Ad
import React from 'react';
class App extends React.Component {
constructor(){
super();
this.update = this.update.bind(this)
this.state = {val: 0}
}
update(){
this.setState({val: this.state.val + 1});
}
componentWillMount() {
console.log("Will mount");
}
render() {
return (
<button onClick={this.update}>
{this.props.txt} - {this.state.val}
</button>
);
}
componentDidMount() {
console.log("mounted");
}
}
App.defaultProps = {txt: 'button'}
I'm trying to follow a tutorial about React, to create a simple button that has a "txt" value that increases by one every time it's clicked. A separate main.js loads this file like this:
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
ReactDOM.render(<App />, document.getElementById('app'));
The error says that the element I'm targeting isn't a string, but 'app' is a string, so I'm not sure why it's not rendering. Any ideas? Here's the index.html file I'm loading:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Lesson 11</title>
</head>
<body>
<div id="app"></div>
<script src="index.js"></script>
</body>
</html>
Thanks for any help!
Ad
Answer
Ad
You need export App
class component from ./App
file
export default class App extends React.Component {
Ad
source: stackoverflow.com
Related Questions
Ad
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
Ad