Answer the question
In order to leave comments, you need to log in
How to pass the response to another component?
Hello everyone
At the front, I almost never wrote anything, so don't judge strictly, I'm just learning to
have a component, a button through which I receive data from the server:
const baseURL = "http://localhost:4000/test";
import React from 'react';
import axios from 'axios';
import ImageComponent from "./imageComponent";
class ButtonComponent extends React.Component {
constructor(props){
super(props);
this.state = {
lalka: '',
isLoading: false,
};
this.click = this.click.bind(this);
}
click() {
this.setState({ isLoading: true });
axios.get(baseURL, )
.then((response) => {
this.setState({ lalka : response.data });
})
.catch((err) => {
console.log(err);
});
}
render() {
return (
<div>
<button onClick={this.click} > click me </button>
</div>
);
}
}
export default ButtonComponent;
import React, { Component } from 'react';
class ImageComponent extends Component {
render() {
return (
<React.Fragment>
<p>{this.props.data}</p>
</React.Fragment>
);
}
}
export default ImageComponent;
Answer the question
In order to leave comments, you need to log in
Let the button props
accept a callback, which it will call when the data is received and will pass this data into the arguments. You need to create a callback in the component in which you want to receive data and set the state with data in this callback
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question