This is my sorting.js file and I am trying to access the resetArray of this file from a onClick button listener of App.js. But unable to do it. Please help.
export default class sorting extends React.Component {
constructor() {
super();
this.state = {
array: [],
};
}
resetArray() {
var array = [];
for (let i = 0; i < 80; i++) {
array.push(this.randomNumber(10, 350));
}
this.setState({ array });
}
}
This is my App.js file
import React from 'react';
import Sorting from './sorting/sorting.js';
class App extends React.Component {
render() {
return (
<div>
<Sorting />
<button onClick={() => this.resetArray()}>
</div>
)
}
}
You need to bring your state up in the hierarchy, in this case, array to your App's state instead of Sorting. Also, you can declare your function in the App.js and pass it as a prop to Sorting.js and call it from there.
I have refactored your code to make it work.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments