Reactjs 如何导出功能组件函数?
给定以下功能组件,如何导出Reactjs 如何导出功能组件函数?,reactjs,Reactjs,给定以下功能组件,如何导出someFunction,而不将其分解为单独的文件,以便对其进行测试 const MyComponent = () => { const someFunction = () => { ... return someValue; }; const [value, setValue] = useState(0); useEffect(() => { setValue(someFunction()); },
someFunction
,而不将其分解为单独的文件,以便对其进行测试
const MyComponent = () => {
const someFunction = () => {
...
return someValue;
};
const [value, setValue] = useState(0);
useEffect(() => {
setValue(someFunction());
}, []);
return (
<div>
...
</div>
);
};
constmycomponent=()=>{
常量someFunction=()=>{
...
返回一些值;
};
const[value,setValue]=useState(0);
useffect(()=>{
setValue(someFunction());
}, []);
返回(
...
);
};
在函数声明前添加导出似乎不起作用。如@yury tarabanko所述,
export
关键字只能在模块范围内使用,即文件根目录
export const someFunction=()=>{
...
返回一些值;
};
导出常量MyComponent=()=>{
const[value,setValue]=useState(0);
useffect(()=>{
setValue(someFunction());
}, []);
返回(
...
);
};
只需执行导出默认函数FunctionName(){}
或
constmycomponent=()=>{
常量someFunction=()=>{
...
返回一些值;
};
const[value,setValue]=useState(0);
useffect(()=>{
setValue(someFunction());
}, []);
返回(
...
);
};
导出默认Mycomponent
您需要在模块范围内定义它,并使用export
关键字这不会起作用,因为该函数仍然只能在内部访问
const MyComponent = () => {
const someFunction = () => {
...
return someValue;
};
const [value, setValue] = useState(0);
useEffect(() => {
setValue(someFunction());
}, []);
return (
<div>
...
</div>
);
};
export default Mycomponent