简体   繁体   中英

React can't render, got Unterminated JSX contents error

class HelloWorldComponent extends React.Component {

  render() {
    const person = {"name":"james"};
    return (      
      <input type="text" placeholder="name" value={person.name}>      
    );
  }
}

React.render(
  <HelloWorldComponent />, document.getElementById('react_example')
);

http://jsbin.com/lalomigufo/edit?js,console,output

Not sure what is wrong, I got error of Unterminated JSX contents when I try to exam my code in https://babeljs.io

Input is a self closing tag. You need to add the "/" at the end of the input element:

return (      
  <input type="text" placeholder="name" value={person.name} />      
);

As the document from facebook :

In HTML, form elements such as , , and typically maintain their own state and update it based on user input. In React, mutable state is typically kept in the state property of components, and only updated with setState()

So, your code put the constant value to value attribute, it's wrong, this value cannot be changed and you can type anything.

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