提问者:小点点

如何在函数组件中使用类组件函数


我有这个类组件

export class Test extends React.Component{

    constructor(props){
        super(props);
        this.state = {
            array:[],
        };
    }
    componentDidMount(){
        this.resetArray();
    }
    resetArray(){
        let n = 100;
          const array = [];
          console.log("Reset Array");
          for(let i = 0; i < n; ++i){
              array.push(randomIntFromInterval(10,300));
          }
          this.setState({array});
      }
}

我想在函数组件中使用这个名为resetArray的函数。有没有什么办法。。。


共1个答案

匿名用户

创建另一个js文件,将resetArray函数的代码作为箭头函数包含在其中,并在需要的其他函数组件中作为组件调用该函数。