Javascript 如何在ReactJS中重新渲染?
我有以下HTML:Javascript 如何在ReactJS中重新渲染?,javascript,reactjs,Javascript,Reactjs,我有以下HTML: <!DOCTYPE html> <html> <head> <title>The game</title> </head> <body> <div id="target"></div> <script src="//fb.me/react-0.12.1.js"></script&g
<!DOCTYPE html>
<html>
<head>
<title>The game</title>
</head>
<body>
<div id="target"></div>
<script src="//fb.me/react-0.12.1.js"></script>
<script src="//fb.me/JSXTransformer-0.12.1.js"></script>
<script type="text/jsx">
React.render(
<h1>Hello, world! {new Date().toISOString()}</h1>,
document.getElementById('target')
);
</script>
</body>
</html>
游戏
反应(
你好,世界!{new Date().toISOString()},
document.getElementById('target')
);
这是在浏览器中呈现类似“Hello,world!2014-12-16T18:45:22.022Z”的字符串,但除非重新加载页面,否则不会更新
在这种情况下,如何让XYZ动态加载,使日期字符串保持最新(没有双关语)
谢谢,收到反馈:
我的代码只呈现了一次,因为React.render()只被调用了一次。我想要的行为可以通过将React.render()调用放在一个间隔内来实现。渲染应该是纯的(如果输入相同,则返回相同的结果),因此React的方法是这样做:
var Hello = React.createClass({
componentDidMount: function() {
this.interval = setInterval(this.update, 16),
},
componentWillUnmount: function() {
clearInterval(this.interval);
},
update: function() {
this.setState({date: new Date()});
},
getInitialState: function() {
return {date: new Date()};
},
render: function() {
// This is pure because it always render the same result if this.date doesn't change
return <h1>Hello, world! {this.state.date.toISOString()}</h1>
}
});
React.render(
<Hello />,
document.getElementById('target')
);
var Hello=React.createClass({
componentDidMount:function(){
this.interval=setInterval(this.update,16),
},
componentWillUnmount:function(){
clearInterval(这个.interval);
},
更新:函数(){
this.setState({date:new date()});
},
getInitialState:函数(){
返回{date:new date()};
},
render:function(){
//这是纯粹的,因为如果This.date不更改,它总是呈现相同的结果
返回Hello,world!{this.state.date.toISOString()}
}
});
反应(
,
document.getElementById('target')
);
您是否考虑过接受以下答案之一?