score:1
Accepted answer
have your child component a prop with method onref like follows:
class child extends component {
componentdidmount() {
this.props.onref(this);
}
render() {
return (
<h1 ref={(ref) => { this.ref = ref }}>hello</h1>
);
}
}
then in your parent method you can access child ref using callback as follows:
class parent extends component {
onclick() {
console.log(this.childhoc) // here access the withstyle ref
console.log(this.actualchild) // here access the actual child component ref
}
render() {
return (
<div>
<child ref={childhoc => this.childhoc = childhoc} onref={actualchild => this.actualchild = actualchild} />
<button onclick={this.onclick.bind(this)}>click</button>
</div>
);
}
}
score:2
you can make use of an innerref prop and use it to get the ref of the child like
class child extends component {
componentdidmount() {
this.props.innerref(this);
}
render() {
return (
<h1 ref={(ref)=>{this.ref = ref}}>hello</h1>
);
}
}
export default withstyles(s)(child);
and in parent
class parent extends component {
onclick() {
console.log(this.child) // here access the ref
console.log(this.child.ref) // undefined
}
render() {
return (
<div>
<child innerref={child => this.child = child} />
<button onclick={this.onclick.bind(this)}>click</button>
</div>
);
}
}
Source: stackoverflow.com
Related Query
- how to access refs in parent when child component is exported with withStyles?
- How do I access refs of a child component in the parent component
- How can I access child components values from a parent component when they are added dynamically?
- Access child function from parent using refs when the child is a function component and the parent does not return JSX
- How to access child component functions via refs
- How to access state when component unmount with React Hooks?
- How to re render parent component when anything changes in Child Component?
- How to pass state with parent to child component
- Unable to call child function from parent using refs with functional component
- Is parent component re-rendered when the child component returns with something new?
- ReactJs How to access child components refs from parent
- React - How do i force child components to re render when parent state component changes?
- How to avoid rerender all child components which in loop when parent component state update
- How to communicate from Child Component to Parent Component with React Router
- how to update parent component when changing child component?
- How to show/hide component in parent component via a button in a child component when using a map of buttons?
- How to access component with ref when it wrapped by withRouter in NextJS?
- How to Prevent Child Component Re-Render when Parent state changes
- How to pass function from parent component to child component with type assingable
- How to pass a function as props when both Parent and Child component also a functional component?
- How to access parent component state in child component?
- React functional component - how do I pass refs back to parent when component returns an array of elements?
- How to pass input values from a child Form component to the state of its parent component for submission with react hooks?
- How do I pass a prop to a react component yet not update that prop in the child when parent changes?
- How to access the DOM element of the child component in Preact with hooks?
- How to access data in child component of query called in parent component in rtk query?
- How to disable scroll parent component when child page (popup page) is opened? - CSS
- How to access param in parent component with React Router DOM
- How to setState in child component when child function called from parent component in ReactJS
- How to get parent props in child component when using styled component in react
More Query from same tag
- Onclick event in For loop in react
- useEffect doesn't work correctly even when setting the dependency
- What is the proper way to calculate price given checkboxes and quantity within React?
- React Router with different paths on same component: does not re-initialize component (useEffect [] not called)
- How to rename iterably object properties
- Css - how to make the slide animation change direction
- ES6 Import some functions as a object
- Error: Invariant failed: You should not use <Link> outside a <Router> Anyone?
- How to use react router with Algolia search hits?
- map function does not return anything in reactjs
- React dynamic nested form input
- React Checkbox Group - Set initial state from API
- Unable to expose a component library in react with webpack and babel
- How to fetch just one time ( stop fetching data after one is fetched )
- What is the correct way to update state and render the component?
- i want to insert <br /> tag in this map()
- React loses focus on input after the first character
- The state of the application is not updated instantly in the code via props in React, MobX
- how do you create a war for spring boot / react application
- I can't use new feature ES 2020 in my next.js app
- How to pass parameters in API request in React?
- React giving error when used connect function to provide state to Hoc
- how can I change props of component that pass from higher component . reactjs
- React- How to re-render view instead of appending it?
- React sends me to 404 when route exist
- Graph streaming real-time data with react and chartjs
- react.js TypeError: lights.map is not a function
- Trying to add one letter at a time to state in React
- possible replace Object.keys(myObj) with chain notation?
- How to scroll a div automatically with button onClick