Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何从外部到内部将数据动态地设置到组件中?_Javascript_Reactjs - Fatal编程技术网

Javascript 如何从外部到内部将数据动态地设置到组件中?

Javascript 如何从外部到内部将数据动态地设置到组件中?,javascript,reactjs,Javascript,Reactjs,我需要从组件中动态设置数据,如下所示: var dynamicData=0; 设置间隔(()=>{ dynamicATA=(Math.random()*100).toFixed(0);/??? }, 3000); var Hello=React.createClass({ render:function(){ 返回{this.props.test}; } }); ReactDOM.render( , document.getElementById('容器') );您应该在setInterval

我需要从组件中动态设置数据,如下所示:

var dynamicData=0;
设置间隔(()=>{
dynamicATA=(Math.random()*100).toFixed(0);/???
}, 3000);
var Hello=React.createClass({
render:function(){
返回{this.props.test};
}
});
ReactDOM.render(
,
document.getElementById('容器')

);
您应该在
setInterval
回调中再次调用
ReactDOM.render
方法。它看起来效率很低,但React足够聪明,可以重用安装的组件并使用新的道具进行更新

从:

如果ReactElement之前已呈现到容器中,则将对其执行更新,并仅在必要时对DOM进行修改,以反映最新的React组件

var dynamicData=0;
设置间隔(()=>{
dynamicATA=(Math.random()*100).toFixed(0);/???
ReactDOM.render(
,
document.getElementById('容器')
);
}, 1000);
var Hello=React.createClass({
render:function(){
返回{this.props.test};
}
});
ReactDOM.render(
,
document.getElementById('容器')
);

您应该在
setInterval
回调中再次调用
ReactDOM.render
方法。它看起来效率很低,但React足够聪明,可以重用安装的组件并使用新的道具进行更新

从:

如果ReactElement之前已呈现到容器中,则将对其执行更新,并仅在必要时对DOM进行修改,以反映最新的React组件

var dynamicData=0;
设置间隔(()=>{
dynamicATA=(Math.random()*100).toFixed(0);/???
ReactDOM.render(
,
document.getElementById('容器')
);
}, 1000);
var Hello=React.createClass({
render:function(){
返回{this.props.test};
}
});
ReactDOM.render(
,
document.getElementById('容器')
);

您可以使用全局事件发射器,并通过
道具传递它,组件将侦听更改并更新其状态

const dynamicData=neweventemitter();
设置间隔(()=>{
设newValue=(Math.random()*100).toFixed(0);
emit('change',newValue)
}, 3000);
var Hello=React.createClass({
getInitialState:()=>({test:0}),
handleChange:函数(newValue){
this.setState({test:newValue})
},
componentDidMount:function(){
this.props.test.on('change',this.handleChange)
},
componentWillUnmount:function(){
this.props.test.removeListener('change',this.handleChange)
},
render:function(){
返回{this.state.test};
}
});
ReactDOM.render(
,
document.getElementById('容器')
);

您可以使用全局事件发射器,并通过
道具传递它,组件将侦听更改并更新其状态

const dynamicData=neweventemitter();
设置间隔(()=>{
设newValue=(Math.random()*100).toFixed(0);
emit('change',newValue)
}, 3000);
var Hello=React.createClass({
getInitialState:()=>({test:0}),
handleChange:函数(newValue){
this.setState({test:newValue})
},
componentDidMount:function(){
this.props.test.on('change',this.handleChange)
},
componentWillUnmount:function(){
this.props.test.removeListener('change',this.handleChange)
},
render:function(){
返回{this.state.test};
}
});
ReactDOM.render(
,
document.getElementById('容器')
);