14

http://codepen.io/JessieZhou/pen/VPgMdP ,Here is a demo using React in CodePen, but the browser gives an error "Uncaught ReferenceError: Component is not defined". However, if I insert a line "import {Component} from 'react'" in the first line, the error will be "Uncaught ReferenceError: require is not defined". Is it possible that the usage of 'class' causes this problem?

Here is my code:

//import {Component} from 'react'
class MyInput extends Component{
   constructor(props){
     super(props);
     this.handleChange = this.handleChange.bind(this);
   }

   handleChange(e){
     this.props.update(e.target.value);
   }

   render(){
     return <input onChange={this.handleChange} type="text"/>
   }
}
ReactDOM.render(MyInput, document.getElementById('myinput'));

Here is my javascript settings in CodePen: javascript settings in codepen

7 Answers 7

7

Reason is Component is part of React, to access that you need to use React.Component, if you directly want to use Component, then first import it from react, like this:

import {Component} from 'react';

Use this:

class MyInput extends React.Component{
    constructor(props){
    super(props);
    this.handleChange = this.handleChange.bind(this);
  }

  handleChange(e){
    console.log('e', e.target.vaule);
  }
  render(){
    return <input onChange={this.handleChange} type="text"/>
  }
}
ReactDOM.render(<MyInput/>, document.getElementById('myinput'));

Check codepen

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you very much! Use React.Component will work, but if I want to use Component, "import {Component} from 'react'" is still not working, the error is "Uncaught ReferenceError: require is not defined"...
use this line in ur project, it will work, in codepen we used to use lib links, that's why we have to use complete React.Component.
Omit the import line
This is useless. It doesn't solve the 'require is not defined' error.
4

Instead of using import, use destructuring assignments to get React.Component. After adding react to codepen through js settings, it executes the script which will make React available on global scope, window.

const {Component} = React;
class MyInput extends Component{
    //Component code
}

1 Comment

This should be the accepted answer
3

Nowadays it is possible to do direct ESM imports from Node packages to Codepen code:

import { default as React } from 'https://cdn.skypack.dev/[email protected]';
import { default as ReactDOM } from 'https://cdn.skypack.dev/[email protected]';

Comments

2

I noticed that process.env.NODE_ENV is undefined in ReactDOM 16.2 js file, if you import the CDN from Quick-add.
The solution is to use the development react and ReactDOM modules from unpkg.com:

//unpkg.com/react/umd/react.development.js
//unpkg.com/react-dom/umd/react-dom.development.js

There is the example works on React 16.2: CodePen

Comments

1

Component is a subclass of react. So either you import it or use React.Component During render you have to use jsx MyInput wont work. <MyInput/> will work

class MyInput extends React.Component{
    constructor(props){
    super(props);
    this.handleChange = this.handleChange.bind(this);
  }

  handleChange(e){
    this.props.update(e.target.value);
  }
  render(){
    return <input onChange={this.handleChange} type="text"/>
  }
}
ReactDOM.render(<MyInput/>, document.getElementById('myinput'));

Comments

1

You can do class MyInput extends React.Component or switch to Webpackbin

1 Comment

Thank you very much~ I will try Webpackbin later~
0

You have to extend React.Component, not just Component.

And you have to render <MyInput /> instead of MyInput.

Try this instead

class MyInput extends React.Component{
  constructor(props){
    super(props);
    this.handleChange = this.handleChange.bind(this);
  }

  handleChange(e){
    this.props.update(e.target.value);
  }
  render(){
    return <input onChange={this.handleChange} type="text"/>
  }
}

ReactDOM.render(<MyInput />, document.getElementById('myinput'));

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.