score:1
Your second component must invoke asyncFuncion
, and then inside asyncFuncion
you can call the callThis
function from the props
class SecondComponent extends Component {
async asyncFunction () {
const data = await getDataFromApi()
this.props.onDataReceived(data)
}
render () {
return <button onClick={() => this.asyncFuncion()} />
}
}
and do not forget to bind that callThis
as well, or just use fat arrow function:
class MainMenu extends Component {
callThis = (data) => {
console.log(data)
}
score:1
this is how you can receive data/use methods from parent passed props:
async asyncFunction () {
const data = await getDataFromApi()
// call the function from first component here...
this.props.onDataReceived(data);
}
score:1
On your first component, you are sending a props to your second components. Here is the documentation : https://reactjs.org/docs/components-and-props.html
To access onDataReceived
in your second component you could write :
async asyncFunction () {
const data = await getDataFromApi()
this.props.onDataReceived(data);
}
Source: stackoverflow.com
Related Query
- Call a method from another Component after the Async function reactjs
- ReactJS how to call a component function from another function on the same file?
- ReactJS - Call One Component Method From Another Component
- How to call another component from onClick function in ReactJS
- Reactjs call function from another component
- ReactJs : Call method from another component having no relation (parent-child)
- Reactjs - Update state of parent after obtaining calculated value from another component function
- Set Recat LInk to Button when button click call the function & after Navigite to Another Component
- how can i send an api call from the supreme function to another component called product to change a mock data called cart: true and display in cart
- How do I call setState from another Component in ReactJs
- How do I call a method from another class component in React.js
- React - Triggering a component method from another component, both belonging in the same render()
- Is it possible to call a reducer function from another reducer function (within the same slice) using Redux Toolkit?
- Call function from another React component
- Call child component method from parent component in reactjs
- How to call a component function on other component, but from the other component ? React
- React Higher Order Component - call function in the wrapped component from the wrapper component
- Call method from sibling component using ReactJS
- How can I call a component's method from another component in React?
- Rendering part of a React function component after async call gets completed
- react redux async action call next function after finishing the function
- Conditionally check value from context and then navigate after login in the same function call in React
- How to call a component method from another component?
- React/ES6 -> How to call an React component class method from another component?
- Get object array from axios and using return value call another axios function and append the output to first result
- Need Reactjs help on how to properly pass this function from the parent to child button component
- Call server-side function from ReactJS component
- In React, how do I call a function in one component (which is being rendered with React Router) from another component?
- Call normal javascript function from another document in a react component function
- In React, how can a parent component set state from the response an async fetch function performed in a child component?
More Query from same tag
- Can someone provide a "hello world" example of JSX being used with out React?
- How to set the background color of Tab.Navigator?
- How access redux-mapped props from outside a connected component
- WordPress block transforms with InnerBlocks template
- Issue in fetch nested datas from JSON.file
- how to make sure two random numbers are not the same in javascript
- Why is my thunk not working in this fetch statement?
- where does the data of any react form get stored
- Why React.FC doesn't allow me to just return children?
- How to write A React component library that allow scss variables to be overwriten
- How to use useState in React 15 with react-bootstrap
- Firebase transaction in React Native
- Does ReactJS parse object literals into strings?
- Fri May 29 2020 06:35:59 GMT+0530 (India Standard Time) i want India Standard Time as IST in react js
- How to make an icon clickable so it renders data below
- Why does my React Dom render method not work?
- React PropTypes validation for arrayOf required elements not working properly
- Condense multiple conditional statements into one switch in styled-components?
- Is it a good idea to add the index to a key in React?
- How to redirect a user based on their referrer using React Router
- React progress element has a bug on IE10
- Can't set props to state using useState React
- Redux Action not hitting Reducer [React]
- How to update nested object in redux or redux-toolkit?
- Get output from user typed code JavaScript
- Remove entire object from a json array javascript
- How to nest Context and MUI's theme provider?
- connect and withRouter issue
- How to store specific API data in local storage and show those data to another page in ReactJs?
- TypeError: Cannot read property 'map' of undefined in react with UseState