Javascript reactjs:UncaughtTypeError:无法读取属性';价值';未定义的
我从这样的东西中获取reactJavascript reactjs:UncaughtTypeError:无法读取属性';价值';未定义的,javascript,reactjs,Javascript,Reactjs,我从这样的东西中获取react渲染后出错 return ( React.createElement('form', { onSubmit: this.onSubmit, className: 'ContactForm', noValidate: true },
渲染后出错
return (
React.createElement('form', {
onSubmit: this.onSubmit,
className: 'ContactForm',
noValidate: true
},
React.createElement('input', {
type: 'text',
className: errors.name && 'ContactForm-error',
placeholder: 'Name',
onInput: this.onNameInput,
value: this.props.value.name,
}),
React.createElement('button', {
type: 'submit'
}, "Add Contact")
)
);
function create_input_element(type, fieldname){
var capital_fieldname = capitalize(fieldname);
return React.createElement('input', {
type: type,
className: errors[fieldname] && 'ContactForm-error',
placeholder: capitalize(capital_fieldname),
onInput: this['on' + capital_fieldname + 'Input'],
value: this.props.value[fieldname],
autoFocus: true,
})
}
create_input_element('text', 'name')
像这样的事情
return (
React.createElement('form', {
onSubmit: this.onSubmit,
className: 'ContactForm',
noValidate: true
},
React.createElement('input', {
type: 'text',
className: errors.name && 'ContactForm-error',
placeholder: 'Name',
onInput: this.onNameInput,
value: this.props.value.name,
}),
React.createElement('button', {
type: 'submit'
}, "Add Contact")
)
);
function create_input_element(type, fieldname){
var capital_fieldname = capitalize(fieldname);
return React.createElement('input', {
type: type,
className: errors[fieldname] && 'ContactForm-error',
placeholder: capitalize(capital_fieldname),
onInput: this['on' + capital_fieldname + 'Input'],
value: this.props.value[fieldname],
autoFocus: true,
})
}
create_input_element('text', 'name')
问题出在this.props.value[fieldname]
,甚至this.props.value.name
也会出现同样的错误
确切的代码是
<head>
<style>
body {
font-family: Tahoma, sans-serif;
margin: 0;
}
.ContactView-title {
font-size: 24px;
padding: 0 24px;
}
.ContactView-list {
list-style: none;
margin: 0;
padding: 0;
border-top: 1px solid #f0f0f0;
}
.ContactItem {
margin: 0;
padding: 8px 24px;
border-bottom: 1px solid #f0f0f0;
}
.ContactItem-email {
font-size: 16px;
font-weight: bold;
margin: 0;
}
.ContactItem-name {
font-size: 14px;
margin-top: 4px;
font-style: italic;
color: #888;
}
.ContactItem-description {
font-size: 14px;
margin-top: 4px;
}
.ContactForm {
padding: 8px 24px;
}
.ContactForm>input {
display: block;
width: 240px;
padding: 4px 8px;
margin-bottom: 8px;
border-radius: 3px;
border: 1px solid #888;
font-size: 14px;
}
.ContactForm>input.ContactForm-error {
border-color: #b30e2f;
}
</style>
<meta name="description" content="Ridiculously Simple Forms with Raw React - Exercise Two">
<script src="https://cdn.rawgit.com/zloirock/core-js/master/client/shim.min.js"></script>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<div id="react-app"></div>
<script src="https://cdn.jsdelivr.net/react/0.14.0-rc1/react.js"></script>
<script src="https://cdn.jsdelivr.net/react/0.14.0-rc1/react-dom.js"></script>
<script>
/*
* Components
*/
function capitalize(str){
return str.charAt(0).toUpperCase() + str.substring(1)
}
var ContactForm = React.createClass({
propTypes: {
value: React.PropTypes.object.isRequired,
onChange: React.PropTypes.func.isRequired,
onSubmit: React.PropTypes.func.isRequired,
},
onEmailInput: function(e) {
this.props.onChange(Object.assign({}, this.props.value, {
email: e.target.value
}));
},
onNameInput: function(e) {
this.props.onChange(Object.assign({}, this.props.value, {
name: e.target.value
}));
},
onSubmit: function(e) {
e.preventDefault();
this.props.onSubmit();
},
render: function() {
var errors = this.props.value.errors || {};
function create_input_element(type, fieldname){
var capital_fieldname = capitalize(fieldname);
return React.createElement('input', {
type: type,
className: errors[fieldname] && 'ContactForm-error',
placeholder: capitalize(capital_fieldname),
onInput: this['on' + capital_fieldname + 'Input'],
value: this.props.value[fieldname],
autoFocus: true,
})
}
return (
React.createElement('form', {
onSubmit: this.onSubmit,
className: 'ContactForm',
noValidate: true
},
create_input_element('email', 'email'),
create_input_element('text', 'name'),
React.createElement('button', {
type: 'submit'
}, "Add Contact")
)
);
},
});
var ContactItem = React.createClass({
propTypes: {
name: React.PropTypes.string.isRequired,
email: React.PropTypes.string.isRequired,
},
render: function() {
return (
React.createElement('li', {
className: 'ContactItem'
},
React.createElement('h2', {
className: 'ContactItem-email'
}, this.props.email),
React.createElement('span', {
className: 'ContactItem-name'
}, this.props.name)
)
);
},
});
var ContactView = React.createClass({
propTypes: {
contacts: React.PropTypes.array.isRequired,
newContact: React.PropTypes.object.isRequired,
onNewContactChange: React.PropTypes.func.isRequired,
onNewContactSubmit: React.PropTypes.func.isRequired,
},
render: function() {
var contactItemElements = this.props.contacts
.map(function(contact) {
return React.createElement(ContactItem, contact);
});
return (
React.createElement('div', {
className: 'ContactView'
},
React.createElement('h1', {
className: 'ContactView-title'
}, "Contacts"),
React.createElement('ul', {
className: 'ContactView-list'
}, contactItemElements),
React.createElement(ContactForm, {
value: this.props.newContact,
onChange: this.props.onNewContactChange,
onSubmit: this.props.onNewContactSubmit,
})
)
);
},
});
/*
* Constants
*/
var CONTACT_TEMPLATE = {
name: "",
email: "",
description: "",
errors: null
};
/*
* Model
*/
// The app's complete current state
var state = {};
// Make the given changes to the state and perform any required housekeeping
function setState(changes) {
Object.assign(state, changes);
ReactDOM.render(
React.createElement(ContactView, Object.assign({}, state, {
onNewContactChange: updateNewContact,
onNewContactSubmit: submitNewContact,
})),
document.getElementById('react-app')
);
}
// Set initial data
setState({
contacts: [{
key: 1,
name: "James K Nelson - Front End Unicorn",
email: "james@jamesknelson.com"
}, {
key: 2,
name: "Jim",
email: "jim@example.com"
}, ],
newContact: Object.assign({}, CONTACT_TEMPLATE),
});
/*
* Actions
*/
function updateNewContact(contact) {
setState({
newContact: contact
});
}
function submitNewContact() {
var contact = Object.assign({}, state.newContact, {
key: state.contacts.length + 1,
errors: {}
});
if (!/.+@.+\..+/.test(contact.email)) {
contact.errors.email = ["Please enter your new contact's email"];
}
if (!contact.name) {
contact.errors.name = ["Please enter your new contact's name"];
}
setState(
Object.keys(contact.errors).length === 0 ?
{
newContact: Object.assign({}, CONTACT_TEMPLATE),
contacts: state.contacts.slice(0).concat(contact),
} :
{
newContact: contact
}
);
}
</script>
</body>
身体{
字体系列:Tahoma,无衬线;
保证金:0;
}
.ContactView标题{
字体大小:24px;
填充:0 24px;
}
.联系人查看列表{
列表样式:无;
保证金:0;
填充:0;
边框顶部:1px实心#f0;
}
.联系人项目{
保证金:0;
填充:8px 24px;
边框底部:1px实心#f0;
}
.ContactItem电子邮件{
字体大小:16px;
字体大小:粗体;
保证金:0;
}
.联系人项目名称{
字体大小:14px;
利润上限:4倍;
字体:斜体;
颜色:#888;
}
.联系人项目说明{
字体大小:14px;
利润上限:4倍;
}
.联络表格{
填充:8px 24px;
}
.联系人表单>输入{
显示:块;
宽度:240px;
填充:4px8px;
边缘底部:8px;
边界半径:3px;
边框:1px实心#888;
字体大小:14px;
}
.ContactForm>input.ContactForm-error{
边框颜色:#b30e2f;
}
JS-Bin
/*
*组成部分
*/
函数大写(str){
返回str.charAt(0).toUpperCase()+str.substring(1)
}
var ContactForm=React.createClass({
道具类型:{
值:React.PropTypes.object.isRequired,
onChange:React.PropTypes.func.isRequired,
提交:React.PropTypes.func.isRequired,
},
onEmailInput:函数(e){
this.props.onChange(Object.assign({},this.props.value{
电子邮件:e.target.value
}));
},
onNameInput:函数(e){
this.props.onChange(Object.assign({},this.props.value{
名称:e.target.value
}));
},
提交人:函数(e){
e、 预防默认值();
this.props.onSubmit();
},
render:function(){
var errors=this.props.value.errors | |{};
函数创建\输入\元素(类型、字段名){
var capital_fieldname=资本化(fieldname);
返回React.createElement('input'{
类型:类型,
类名:错误[fieldname]&&“ContactForm错误”,
占位符:大写(大写+字段名),
onInput:this['on'+大写字母+字段名+'Input'],
value:this.props.value[fieldname],
自动对焦:对,
})
}
返回(
React.createElement('form'{
onSubmit:this.onSubmit,
类名:“ContactForm”,
诺瓦利达:对
},
创建输入元素(“电子邮件”、“电子邮件”),
创建输入元素(“文本”、“名称”),
React.createElement('按钮'{
键入:“提交”
}“添加联系人”)
)
);
},
});
var ContactItem=React.createClass({
道具类型:{
名称:React.PropTypes.string.isRequired,
电子邮件:React.PropTypes.string.isRequired,
},
render:function(){
返回(
React.createElement('li'{
类名:“ContactItem”
},
React.createElement('h2'{
类名:“ContactItem电子邮件”
},此.props.email),
React.createElement('span'{
className:“ContactItem名称”
},this.props.name)
)
);
},
});
var ContactView=React.createClass({
道具类型:{
联系人:React.PropTypes.array.isRequired,
newContact:React.PropTypes.object.isRequired,
onNewContactChange:React.PropTypes.func.isRequired,
onNewContactSubmit:React.PropTypes.func.isRequired,
},
render:function(){
var contactItemElements=this.props.contacts
.map(功能(联系人){
返回React.createElement(ContactItem,contact);
});
返回(
React.createElement('div'{
类名:“ContactView”
},
React.createElement('h1'{
类名:“ContactView标题”
}“联系人”),
React.createElement('ul'{
类名:“联系人视图列表”
},contactItemElements),
React.createElement(ContactForm{
值:this.props.newContact,
onChange:this.props.onNewContactChange,
提交:this.props.onNewContactSubmit,
})