简体   繁体   English

React无法渲染,出现了未终止的JSX内容错误

[英]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 http://jsbin.com/lalomigufo/edit?js,控制台,输出

Not sure what is wrong, I got error of Unterminated JSX contents when I try to exam my code in https://babeljs.io 不知道出什么问题了,尝试在https://babeljs.io中检查代码时出现了Unterminated JSX contents错误

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 : 作为来自facebook的文件:

In HTML, form elements such as , , and typically maintain their own state and update it based on user input. 在HTML中,表单元素(例如,和)通常会维护自己的状态并根据用户输入对其进行更新。 In React, mutable state is typically kept in the state property of components, and only updated with setState() 在React中,可变状态通常保留在组件的state属性中,并且仅使用setState()更新

So, your code put the constant value to value attribute, it's wrong, this value cannot be changed and you can type anything. 因此,您的代码将常量值设置为value属性,这是错误的,无法更改此值,您可以键入任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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