DEV Community

CristianCuartas
CristianCuartas

Posted on

Pass a state from one class to another class in REACT JS.

Hello, I need to pass a state from one class to another class.

Can I do it with a callback?

You help me?

Example:
class SearchNav extends Component {
constructor(props) {
super(props);
this.state = {
q: '',
param: {},
getData: []
};
}

getDataSearch = () => {
fetch(url + ${this.state.q})
.then(res => res.json())
.then(data => {
this.setState({ getData: data.hits });
console.log(this.state.getData);
})
.catch(Error => {
console.log(Error);
});
};
...
I need pass state =>'getData' to other class.

Top comments (0)