好的,所以我開始讓我的頭繞着ReactJs,但一直被一個簡單的概念困擾着,它是一個簡單的舊jQuery的簡單概念。從一個反應組件向另一個反應組件注入內容onClick
我希望能夠在點擊事件發生在另一個元素上時添加到屏幕上的一個元素的內容。在react tutorial之後,我完全理解他們已經實現了添加到評論列表的方式,評論列表是父母的設置狀態的孩子..但肯定這不是唯一的方式,因爲它感覺非常僵硬和僵化。
下面是我想解釋的一個簡單模型。在按鈕的點擊,我想注入新的內容到DIV ID爲「newComments」 .. JSBin:http://jsbin.com/vokufujupu/1/edit
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JS Bin</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.13.3/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.13.3/JSXTransformer.js"></script>
</head>
<body>
<div id="content"></div>
<script type="text/jsx">
var InputBox = React.createClass({
clickHandler: function(){
//Append a new string to the end of the existing copy in the copy box
alert('after this alert the value of the button should be appended to the content of the div#newComments');
},
render: function() {
return (
<div classNmae="copyBox">
<input type="button" name="inputButton" value="click me button" className="bbbc"
onClick={this.clickHandler} />
</div>
);
}
});
var CopyBox = React.createClass({
render: function() {
return (
<div classNmae="copyBox">
<p>div#newComments:</p>
<div id="newComments"></div>
</div>
);
}
});
var Page = React.createClass({
render: function() {
return (
<div>
<CopyBox/>
<InputBox/>
</div>
);
}
});
React.render(
<Page/>,
document.getElementById('content')
);
</script>
<!-- The equiv in plain old js -->
<script type="text/javascript">
function newContent(obj){
document.getElementById('vanBox').innerHTML = document.getElementById('vanBox').innerHTML + obj.value;
}
</script>
<div id="vanilaJsEquiv">
<div id="vanBox"></div>
<input type="button" value="ClickyClik" onclick="newContent(this)"/>
</div>
</body>
</html>
我一直在四處狩獵谷歌及yonks的文檔,並不能找到答案..