score:1
This 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.
componentWillMount = () => {
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.
score:-1
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>
)
}
}
score:-1
There are 2 approaches in your case:
- Whenever the global variable change, re-render the Example component. This is a costly operation.
- Example component should listen for a change in window.something. For that there should be some magic which updates your Example component state when window.something changes. Then the component gets updated smoothly.
I suggest second method and use a store system like Redux to implement that magic.
Source: stackoverflow.com
Related Query
- Updating the react component state when a global variable changes
- React hook changing the state variable of wrong component when file input
- HighCharts not updating when the state changes in React
- Input element's value in react component is not getting re-rendered when the state changes
- react easy-peasy component is not rerendered when the global state is modified
- Updating react component when redux state changes
- How to render component conditionally when leaf value of global state changes using react hooks
- React component is not getting updated when state changes into the redux
- React Video Component not updating when state changes
- React does not re-render component when the state changes
- When state changes the right component is not getting displayed in React
- React component not updating when store state has changed
- Accessing the State of a Functional Component with React Hooks when Testing with Enzyme
- React leaflet center attribute does not change when the center state changes
- Component not updating when I change the props that I pass to it in React
- React state changes when its assigned variable changes
- Change the state when clicking outside a component in React
- React component interaction and global state changes
- React is not updating when Redux state is changed below the first level
- component not re-rendering when updating the state in mobx
- The Graph component in react-graph-vis doesn't reload when the state changes
- Too many re-renders. React limits the number of renders to prevent an infinite loop. Updating state of a functional component inside the render method
- React: How to show Spinner & Component (at the same time) when State Changes using `useEffect`
- React component rendering even when there is no change in the state value
- Prevent component as array element from re-render when the parent array changes in React
- React Component not updated when state changes
- How to re-render only one component on hover when state changes in React
- React hooks - how to force useEffect to run when state changes to the same value?
- How to update redux state using a react variable passed up to the component from a child
- React.js component not re-rendering children when the useState hook changes state
More Query from same tag
- How to replace a React component
- React setState doesn't rerender in ajax success
- How to pass props in routes?
- styled-components error 'cannot read property of undefined' prop that is being passed
- Always redirected to current route instead of desired route
- Reactjs - Using "this" in components - should it be avoided at all times?
- Conditional rendering with reactjs
- npm WARN checkPermissions Missing write access to /usr/local/lib/node_modules
- Why I can't set and change my entire state with dispatcher like that?
- How do I get a collection of documents all at once using an array of IDs using firebase?
- which hooks we use for update and delete in react?
- React/JS/TS - Adding prop to component by location
- How do I change a Formik TextField value from the outside of the From?
- React Render UI Before Redirect
- What parameters can React.createElement() take in?
- Cross-origin request blocked, origin 'null' no access
- React Typescript: Passing onClick as a Prop
- Cannot get properties of an object after using filter()
- Apollo GraphQL requests are canceled
- How to cancel the RTK-Query requests
- Passing something to main app.js file in React/Redux
- Hook query retruning empty object inside useEffect but not after it Why?
- Extract class name with styled-components
- Retrieve data from array.map via onClick in react
- React Konva center text in square
- How to populate components on a new page when clicking on component in current page
- onKeyDown event not working on divs in React
- Difference between passing event handler prop as arrow function or function reference
- Cannot read property 'includes' of undefined in reactjs?
- Getting error on console despite handling it (MERN + Redux)