简体   繁体   English

在 Reactjs 中导出默认值

[英]Export default in Reactjs

How am I able to name an exported function?如何命名导出的 function? deafult gives me an error deafult给我一个错误

export default() => {
const [width, setWidth] = useState(0);
const [height, setHeight] = useState(0);

useEffect(() => {
setTimeout(() => {
  setWidth(window.innerWidth);
  setHeight(window.innerHeight);
}, 100);
});

return (
<Layout title="Success!">
  <Container>
    <Confetti width={width} height={height} numberOfPieces={450} />
    <Title>congrats!</Title>
    <Message>Stripe has successfully processed your payment.</Message>
  </Container>
</Layout>
);
}

I would like to name my function default but it gives me an error, is this possible?我想将我的 function 命名为默认值,但它给了我一个错误,这可能吗?

Try this尝试这个

const MyFunction = () => {
const [width, setWidth] = useState(0);
const [height, setHeight] = useState(0);

useEffect(() => {
setTimeout(() => {
  setWidth(window.innerWidth);
  setHeight(window.innerHeight);
}, 100);
});

return (
<Layout title="Success!">
  <Container>
    <Confetti width={width} height={height} numberOfPieces={450} />
    <Title>congrats!</Title>
    <Message>Stripe has successfully processed your payment.</Message>
  </Container>
</Layout>
);
}

export default MyFunction

There are two ways to create an export.有两种方法可以创建导出。

You can do it all at once with a function .您可以使用function一次完成所有操作。

export default function App() {
  return <div />
}

Or you can use an arrow function and do it in two parts.或者您可以使用箭头 function 并分两部分进行。

const App = () => {
  return <div />
}
export default App;

Which one you use is mostly up to preference.您使用哪一个主要取决于偏好。 JavaScript users typically prefer the first and TypeScript users the second. JavaScript 用户通常更喜欢第一个,TypeScript 用户通常喜欢第二个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM