This is an intriguing question! At a high level, the problem is that React isn't catching the 'change' event that bubbled up to the top-level component when you clicked the checkbox, because it wasn't instantiated yet on the client side. Your half-solution handles this by manually simulating a call to
onChange. I started thinking that you might need to queue onChange events... but then I realized that React already has everything you need.
The "three-quarters" solution is to simply rename
componentDidMount, and don't even bother calling it manually. As per the docs, in the most recent versions of React,
componentDidMount is only called in the browser, and it's a lifecycle callback for after the component mounts (i.e. when
React.render is about to return). It's the perfect place for your use case. See: http://jsfiddle.net/qdt4z3w9/
That solves your problem of code external to the component itself! But there's still state-setting going on after the original render takes place. Unfortunately, I think this is fundamentally the way React works - in order to be able to match up existing DOM nodes to refs, each component needs to be fully mounted first. But an extra Virtual DOM diff is a small price to pay, since it's designed to be lightning-quick.
Hope this helps!
- How do I sync state from the DOM to my React Component in an Isomorphic Application?
- How do I access the Context and Component state from within a DOM callback in a function React component?
- How to initialize the react functional component state from props
- How to update the state of a sibling component from another sibling or imported component in REACT
- How do I manage state on a React component that can have state changed from the parent or from events upon it?
- I can not get the state from react router Link component using useLocation. So how can I pass it?
- How to pass input values from a child Form component to the state of its parent component for submission with react hooks?
- How to update redux state using a react variable passed up to the component from a child
- How do I keep the state of a React Component after removing others from an array?
- How to change only a part of the state from child component react hooks
- How to set the state of a component with the information obtained from a promise to avoid memory leaks in react
- How to set the state of parent component in react from inside child component
- How can custom Hooks in React have different state names then the state name used by the functional component from which it was called?
- How to set state in parent from one child component and access the state in another child component using react and typescript?
- In React components implemented with hooks, how can the latest component state values be read from within setInterval?
- How do I use react hooks to tidy up a functional component when it unmounts (using values from state in the tidy up)
- How to use react hooks to pass a state from child component to the parent component?
- In React app , I delete my component data on firebase but it does'n delete from my dom even when I change the state
- Making an HTML string from a React component in background, how to use the string by dangerouslySetInnerHTML in another React component
- React, how to access the DOM element in my render function from the same component
- How to set React component state and props from browser
- How to pass the match when using render in Route component from react router (v4)
- How to TEST async calls made in componentDidMount that set the state of React Component
- Reactjs how to change the state of a component from a different component
- How does React know the component is removed from the DOM?
- How to test the state of a functional component in React with Jest (No Enzyme)
- How to get the DOM node from a Class Component ref with the React.createRef() API
- How to maintain react component state when goback from router?
- how and when to call a react component methods after state change from redux
- How do you dynamically insert a React component into the DOM (for instance a dialog)?
More Query from same tag
- How to rerender a component from another component
- How to send URL parameter from react router to a toolbar?
- change opacity after 200ms in component style ReactJS
- How to connect to a sqlite db from a React app?
- DIspatch not firing in React Redux
- Detect if website runs using webview in windows forms
- Is mutating "copy" of initial state inside a passed reducer function in "Redux Toolkits" createSlice() a bad practice?
- React styled-components: refer to other components
- How to test a react connected component and what to test of component?
- Checkbox with unicode symbol for Firefox
- How can we display multiple textarea based on checkbox selection in react hooks?
- How to iterate over multiple arrays in a JSON object in react
- React: Separate array of timestamps into days
- How to properly configure .png images in webpack?
- How to extract only the year from a date string?
- react formik disable submit button with form validity
- How do I go about hiding an element when the user scrolls to a specific point on the page in React?
- JS Unit testing run multiple times with different parameters
- Redux form not getting re-rendered
- async/await for fetching data with spinner
- React.js - CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. [AWS - Lambda - API GATEWAY]
- add popover when event is clicked in react-big-calendar?
- Apollo Client Cache vs. Redux
- How to use actions on Formik forms?
- Select and deselect values with react and hooks
- Gulp babel with babel-preset-react not transforming JSX code
- How to pass a ref into a class function
- React 17.0.1 map is undefined
- Update State component after fetch Redux
- React-router server-side error