我正在尝试编写一个程序,用于显示输入值对输出框的更改,但出现了错误,如“未捕获类型错误:无法读取null的属性'value'”,我不知道这里出了什么错误。
var App =React.createClass({
getInitialState:function(){
return{
value:"My Value"
}
},
updateValue:function(modifiedValue){
this.setState({
value:modifiedValue
})
},
render:function(){
return(
<div className="inputBox container-fluid">
<h1 className="text-center text-primary">Hello FreeCodeCampers !!!</h1>
<div className="row col-md-12">
<InputBox value={this.state.value} updateValue={this.updateValue}/>
<h1>{this.state.value}</h1>
</div>
</div>
);
}
});
var InputBox =React.createClass({
update:function(){
var modifiedValue=ReactDOM.findDOMNode(this.refs.inputValue).value;
this.props.updateValue(modifiedValue);
},
render:function(){
return(
<input type="text" value={this.props.value} onChange={this.update} ref="initialValue"/>
);
}
});
ReactDOM.render(<App />,
document.querySelector("#app")
);
<!DOCTYPE html>
<head>
<meta charset="UTF-8" />
<title>React Tutorial</title>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</head>
<div id="app"></div>
<script src="demo.js" type="text/babel"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.7/react.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.7/react-dom.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.23/browser.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/marked/0.3.2/marked.min.js"></script>
</body>
</html>