【问题标题】:What's the difference between exporting a const function or a plain function? [duplicate]What\'s the difference between exporting a const function or a plain function? [duplicate]
【发布时间】:2022-12-01 21:43:08
【问题描述】:

I see some applications do this:

// App.jsx
export function App() {}

// index.jsx
import { App } from './App'

and others do this:

// App.jsx
export const App = () => {}

// index.jsx
import { App } from './App'

Is there any difference? If there are differences, when would you pick one over the other?

【问题讨论】:

  • It's not the form of function that's the distinction (your first could just as easily be export const App = () => { }; and your second could be export default function App() { }), it's that the first one is anamed export(and associated import) and the other is thedefault export(and associated import). See the linked question's answers (and MDN's documentation) for the distinction between the two.
  • Thanks @T.J.Crowder I didn't write my question properly - I wasn't really wasking about named/default exports, I was more curious about using a const or a function that is exported
  • In that case, see this question's answers for the distinction between an arrow function and a traditional function. (For a React component function, it makes no difference. In other situations, it might. ;-) )
  • (Here's the original dupetarget for anyone who's interested.)

标签: reactjs jsx


【解决方案1】:
猜你喜欢
  • 2022-12-01
  • 1970-01-01
  • 2017-06-22
  • 2022-12-02
  • 2022-12-02
  • 1970-01-01
  • 2021-09-17
  • 2016-11-14
  • 1970-01-01
相关资源
最近更新 更多