admin管理员组文章数量:1356011
I have a global variable that will change often. Let's say it is stored in window.something
. In react I need this change to be reflected in the ponent as well as in its state.
Example code:
class Example extends React.Component {
constructor(props) {
super(props);
this.state = { something: '1'}
}
render() {
return (
<div>
<input value={window.something}
onChange={event => {this.setState({'something': event.target.value})}}
/>
</div>
)
}
}
However the value gets set only for the first time and there is no change as the variable gets updated.
I have a global variable that will change often. Let's say it is stored in window.something
. In react I need this change to be reflected in the ponent as well as in its state.
Example code:
class Example extends React.Component {
constructor(props) {
super(props);
this.state = { something: '1'}
}
render() {
return (
<div>
<input value={window.something}
onChange={event => {this.setState({'something': event.target.value})}}
/>
</div>
)
}
}
However the value gets set only for the first time and there is no change as the variable gets updated.
Share Improve this question asked Mar 28, 2018 at 5:30 sjishansjishan 3,67211 gold badges32 silver badges56 bronze badges 4-
Do you want to use the
window.something
value only for the first time? – salman.zare Commented Mar 28, 2018 at 5:46 - 2 What caused the global variable to change? – Steven Commented Mar 28, 2018 at 5:57
- Try changing the fat arrow function to normal function. Because, using 'this' inside fat arrow function does not work as expected and it refers to the outer object – rahul_sann Commented Mar 28, 2018 at 6:22
-
it should be a
prop
, something like<MyComponent something={window.something} />
Then in your ponentconst MyComponent = ({something}) => { React.useEffect(()=>{ // do something when something changes return () => {} ,[something]}) ... }
– Rafael Mora Commented Oct 14, 2019 at 16:57
3 Answers
Reset to default 1This won't be triggered by a change to a storage object. If you have access to the process that triggered the change, then have it call an update to state. If you don't have access, maybe you could poll for the change every so often.
Personally, I just get it from storage on pages that I need it, or add it to state and use that for the rest of the session.
ponentWillMount = () => {
const foo = JSON.parse(sessionStorage.getItem('foo'));
const bar = JSON.parse(sessionStorage.getItem('bar'));
if (!isEmpty(foo) && !isEmpty(bar)) {
this.props.setFoo(foo);
this.props.setBar(bar);
}
}
Here is a link explaining how to set an event listener on a browser's localStorage.
Something like this?
window.something='1234';
class Example extends React.Component {
constructor(props) {
super(props);
this.state = { something: window.something}
}
render() {
return (
<div>
<input defaultValue={window.something}
value={this.state.something}
onChange={event => {this.setState({'something': event.target.value})}}
/>
</div>
)
}
}
There are 2 approaches in your case:
- Whenever the global variable change, re-render the Example ponent. This is a costly operation.
- Example ponent should listen for a change in window.something. For that there should be some magic which updates your Example ponent state when window.something changes. Then the ponent gets updated smoothly.
I suggest second method and use a store system like Redux to implement that magic.
本文标签: javascriptUpdating the react component state when a global variable changesStack Overflow
版权声明:本文标题:javascript - Updating the react component state when a global variable changes - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744018572a2576790.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论