从外部函数渲染后更改ReactJS类的状态
Altering the state of a ReactJS class after render from an outside function
在学习一些reactjs并努力保持和关注结构(同时看看我可以用reactjss去哪里)时。。我正在努力保持我所知道的标准JavaScript名称空间。。。
我有以下内容,它完美地呈现了初始消息,但是reactTestjsx.hello.randomMsgChange();
在尝试设置已经创建的react类的状态时抛出了一个错误。
有可能用这种方式访问react渲染的类吗?
//general js stuff
var reactTest = {
toolbox: {
shuffle: function(o){
for(var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
return o;
}
}
};
//JSX components
var reactTestjsx = {};
reactTestjsx.hello ={
init: function(){
reactTestjsx.hello.randomMsgChange();
},
randomMsgChange: function(){
setInterval(function(){
var msg = reactTest.toolbox.shuffle([
'hello world',
'hello solar system',
'hello universe'
])[0];
//issue here, cannot access the setState of the "reactTestjsx.hello.show" object
reactTestjsx.hello.show.setState( msg );
},1000)
},
show : React.createClass({
getInitialState: function(){
return {message:'hi world'}
},
render: function() {
return (
<p>{this.state.message}</p>
)
}
})
};
//render the component
React.render(
<reactTestjsx.hello.show/>,
document.querySelector('#content')
);
//call the init to auto switch the message
reactTestjsx.hello.init();
我已经获取了您的代码,并对其进行了一点重构,以便演示一种使代码工作的方法。
http://jsfiddle.net/wiredprairie/7o2sy3k5/
以下是我做过的事情:
- 由于在JavaScript中对名称空间(以及React组件)使用Title case是很常见的,所以我已经做出了更改
- 我为
Components
添加了一个额外的命名空间,并在其中放置了ShowMessage
组件 - 由于ReactJS是虚拟DOM,并且面向"diff",所以我更新了
randomMsgChange
代码,以便在每次更新msg
时重新发送ShowMessage
控件 - 我已经从使用
setState
更改为仅使用普通属性,因为ShowMessage
组件不会修改正在传递的属性的值 - 当您使用JSX时,像
<ShowMessage msg="Yes!"/>
这样的语法实际上是在创建一个名为ReactElement
的类的包装器实例。该类负责创建您指定的类(如ShowMessage
)的实例。因此,要更新ShowMessage
实例的属性,您将重新提交ShowMessage
代码:
//general js stuff
var ReactTest = {
Toolbox: {
shuffle: function(o){
for(var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
return o;
}
}
};
//JSX components
var ReactTestJsx = ReactTestJsx || {};
ReactTestJsx.Hello = {
init: function(){
ReactTestJsx.Hello.randomMsgChange();
},
renderComponent: function(msg) {
//render the component
React.render(
<ReactTestJsx.Components.ShowMessage message={ msg } />, document.querySelector('#content')
);
},
randomMsgChange: function(){
setInterval(function(){
var msg = ReactTest.Toolbox.shuffle([
'hello world',
'hello solar system',
'hello universe'
])[0];
ReactTestJsx.Hello.renderComponent(msg);
},1000)
}
};
ReactTestJsx.Components = {
ShowMessage : React.createClass({
getDefaultProps: function() {
return { message: "hi world" };
},
render: function() {
return (
<p>{this.props.message}</p>
)
}
})
};
ReactTestJsx.Hello.renderComponent();
//call the init to auto switch the message
ReactTestJsx.Hello.init();
相关文章:
- 如何更改reactjs中外部/独立组件的状态或属性
- 从外部函数渲染后更改ReactJS类的状态
- ReactJS:使用浏览器按钮导航时保存状态
- reactjs-redux,是否可以有一个由2个组件使用的状态
- 是否可以在reactjs中设置多个状态变量
- 如果我不应该在组件WillUpdate中调用setState,如何更新状态(使用ReactJS)
- Reactjs:在修改对象之前,是否需要复制处于组件状态的对象
- 如何在reactjs中管理树组件中的状态
- 设置状态PubSub ReactJS和Rails
- 将状态传递到Ajax调用ReactJS和Rails中
- ReactJS-这个状态中的数组在删除时被正确更新,但结果是't,直到以后的状态发生变化
- ReactJS:如何在改变状态后立即更新组件
- 在 ReactJs 状态上将项目添加到数组中,并超时以进行自我删除
- ReactJS状态没有更新
- 更新 ReactJS 状态数组
- 异步设置ReactJS状态
- 更新深层ReactJS状态
- 多个Axios请求进入ReactJS状态
- reactjs状态未通过redux更新
- 将回调绑定到 DynamoDB DocumentClient 的 ReactJS 状态