小编NCa*_*Cal的帖子

将React类函数存储在单独的文件中

有没有办法将react类的函数存储在一个单独的文件中,然后导入这些函数以供使用?想要这样做的原因纯粹是为了减少组件的大小并按类别对功能进行分组.

例如:

import functionIWantToImport from '../functions/functionIWantToImport';
import anotherFunction from '../functions/anotherFunction';

Class Test extends React.Component {
   constructor(props){
       super(props);

       this.state = {};
   }

   //and then include the functions like below:
   functionIWantToImport;
   anotherFunction;
}
Run Code Online (Sandbox Code Playgroud)

我知道我可以使用不属于组件类的辅助函数来完成此操作.但我想用我的组件功能来节省空间,并清理我的代码.

import components class code-cleanup reactjs

4
推荐指数
1
解决办法
1122
查看次数

标签 统计

class ×1

code-cleanup ×1

components ×1

import ×1

reactjs ×1