score:0
ref
is property of each this.props.children
hence you can access ref of child component in parent via ref
property on this.props.children
make sure you access ref after componentdidmount
edit :
try below set of code if this works :
var mychild= react.children.only(this.props.children);
var clone = react.cloneelement(mychild, { ref: "myref" });
score:9
the react's main idea is passing props downward from parent to children (even to deeper levels of children - grandchildren i mean)
therefore, when we want the parent to do something which is triggered from (or belongs to) the children, we can create a callback function in the parent, then pass it down to children as props
for your preference, this is a demonstration on how to pass callback functions downward through many levels of children and how to trigger them:
force react container to refresh data
re-initializing class on redirect
in your case, you can access refs from children components as follows: (using string for ref - as you stated)
parent component:
import react, { component } from 'react';
// import child component here
export default class parent extends component {
constructor(props){
super(props);
// ...
this.getrefsfromchild = this.getrefsfromchild.bind(this);
}
getrefsfromchild(childrefs) {
// you can get your requested value here, you can either use state/props/ or whatever you like based on your need case by case
this.setstate({
myrequestedrefs: childrefs
});
console.log(this.state.myrequestedrefs); // this should have *info*, *contact* as keys
}
render() {
return (
<child passrefupward={this.getrefsfromchild} />
)
}
}
child component:
import react, { component } from 'react';
export default class child extends component {
constructor(props){
super(props);
// ...
}
componentdidmount() {
// pass the requested ref here
this.props.passrefupward(this.refs);
}
render() {
return (
<div classname="homemain">
<section ref="info"> <info/> </section>
<section ref="contact"> <contact /> </section>
</div>
)
}
}
Source: stackoverflow.com
Related Query
- How to get refs from another component in React JS
- how to get a Component variable from another component but their both in the same file React
- how to get selected item in an array from another functional component react
- In a react components, how to get latest data from state once it is changed from another component using redux?
- React : how to get input value from one component to make the ajax call in another component?
- Making an HTML string from a React component in background, how to use the string by dangerouslySetInnerHTML in another React component
- How to update the state of a sibling component from another sibling or imported component in REACT
- How to get value from a react function within a react component
- How to from one component set state another component in react native?
- How to pass data from one component to another in React or React-Redux?
- How do I call/execute a function from another component in react native?
- I can not get the state from react router Link component using useLocation. So how can I pass it?
- How to get result from createAsyncThunk in React component Redux Toolkit
- How to refresh data of one component from another component in react js
- React - Get Div Element from another Component
- How to get a reference to react component from a document event?
- How to send state from Child component to another component in React
- React/ES6 -> How to call an React component class method from another component?
- How to take the input from a react component and pass the length of the input to another react component
- How to get function from parent component in React
- How to get data from table row click using Semantic's React Table Component
- How to use functions from another component in React Typescript
- In React, how do I call a function in one component (which is being rendered with React Router) from another component?
- Get object from another class in component react
- How to stop the previous component from rendering when it is redirected to another component in react
- How to access data from get request made by a React functional component to localhost server using Axios?
- I want to navigate to a page/component where I passed the data from another component using both React hooks and useHistory, but I get undefined
- How to assign value onClick from different component to another component in React
- How to set one component's state from another component in React
- React js - How to get Click event values from child component to parent component?
More Query from same tag
- What are refs in React?
- Is dispatch function returned from useDispatch hook synchronous with async thunk?
- Constructor Method in React
- TypeError: undefined is not a function with React and Express
- React MDBTable render each item in table
- Styling rendered iframe in react
- Using Conditional Logic Inside of an array.map in React
- ApexCharts: change the default blue color of the zoom/pan icons
- How to use regex to split a string by comma or linebreak in javascript?
- socket.io connects with same socket id
- How to get rid of the this warning: "Warning: validateDOMNesting(...)"?
- Why is Heroku telling me it cannot find a package.json in my module when I do a heroku push
- SyntaxError: Expected corresponding JSX closing tag for <Route> with react-router-dom v6
- JavaScript - creating a comma delimited string from a lookup in a heavily nested array
- Why the theme doesn't work in Material-UI?
- React native select: Access key / index of selected option
- How Virtual DOM works in React or Vue
- How to get routes from other location/function in react-router-dom v6?
- How to clear a 'React boostrap-validation' form?
- React Context API - create context from axios response
- Pass down label element as a prop in react
- start antd RangePicker (datepicker) week from monday instead of sunday
- React Native Textinput Flickers when using Redux
- React router and nested routes
- ReactJS passing custom components to a grid
- how do make the text far from the cirlce i tried padding and margin and it didn't work
- How to give a Component as a parameter to the react-router in an application start
- Render data from localStorage with react
- Trigger a method in a functional component after click on a button from a different component
- Can't find module TS2307 in React