我正在使用Docusaurus,它提供了siteConfig.js
作为配置道具。因此,我必须使用此道具来构建我的网站组件。工作代码的格式如下:
const React = require("react");
class SamplePage extends React.Component {
render() {
const siteConfig = this.props.config;
return <div>{siteConfig.title}</div>;
}
}
module.exports = SamplePage;
我在此question中显示了另一段工作代码,但是它使用不同的设置,其中const {useState} = React;
代替了const React = require("react");
和<div id="root">
ReactDOM.render(<SamplePage/>, document.getElementById("root"));
代替module.exports = SamplePage;
。我知道这允许在SE上运行代码段,但是并没有告诉我在该Docusaurus项目的上下文中导入和导出应如何起作用。我想做的是将代码段合并到React.Component
中,或者以其他方式构造此组件以将useState
钩与配置道具一起使用,以断言或拒绝3的isOpen
属性detail
标签,使用2个button
(s)来控制挂钩:
const {useState} = React;
const SamplePage = () => {
const [isOpen, setIsOpen] = React.useState(false);
return (
<div>
<details open={isOpen}>
<summary>
First text detail.
</summary>
<p>testing</p>
</details>
<details open={isOpen}>
<summary>
Second text detail.
</summary>
<p>testing</p>
</details>
<details open={isOpen}>
<summary>
Third text detail.
</summary>
<p>testing</p>
</details>
<button onClick={() => setIsOpen(false)}>Open All Details.</button>
<button onClick={() => setIsOpen(true)}>Close All Details.</button>
</div>
);
}
ReactDOM.render(<SamplePage/>, document.getElementById("root"));
有关代码段:
<div id="root"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.9.0/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.9.0/umd/react-dom.production.min.js"></script>
我的问题是如何组合这些代码段。我试图以许多不同的方式构造此组件,但无法获得button
来激发onClick()
效果。例如,我尝试过:
const React = require("react");
const SamplePage, {useState} = () => {
const [isOpen, setIsOpen] = React.useState(false);
const siteConfig = this.props.config;
return (
<div>
<details open={isOpen}>
<summary>
First text detail.
</summary>
<p>testing</p>
</details>
<details open={isOpen}>
<summary>
Second text detail.
</summary>
<p>testing</p>
</details>
<details open={isOpen}>
<summary>
Third text detail.
</summary>
<p>testing</p>
</details>
<button onClick={() => setIsOpen(false)}>Open All Details.</button>
<button onClick={() => setIsOpen(true)}>Close All Details.</button>
</div>
);
}
module.exports = SamplePage;
由于我实际上不能在当前设置中使用useState
钩子,因此会抛出“无效的钩子调用”。在其他所有构造中,我都得到了意外的标记和参考错误。
答案 0 :(得分:1)
您需要在onClicks中翻转是真/假布尔(打开应为真)
请记住,您的“打开” onClick
会很好地打开下拉菜单,但是“关闭” onClick
仅在设置了“打开” onClick
的情况下才会关闭下拉菜单首先声明为真
如果您需要导出它而不是将其呈现到DOM(并且不能/不想在其他地方更改为ES6 import语句),则将ReactDom.render()
更改为:
module.exports = SamplePage;
完整示例:
const React = require('react')
const {useState} = React;
const SamplePage = (props) => {
const [isOpen, setIsOpen] = useState(false);
const siteConfig = props.config
return (
<div>
<details open={isOpen}>
<summary>
First text detail.
</summary>
<p>testing</p>
</details>
<details open={isOpen}>
<summary>
Second text detail.
</summary>
<p>testing</p>
</details>
<details open={isOpen}>
<summary>
Third text detail.
</summary>
<p>testing</p>
</details>
<button onClick={() => setIsOpen(true)}>Open All Details.</button>
<button onClick={() => setIsOpen(false)}>Close All Details.</button>
</div>
);
}
module.exports = SamplePage;
可运行代码段:
// const React = require('react')
const {useState} = React; //refer to above note
const SamplePage = (props) => {
const [isOpen, setIsOpen] = useState(false);
const siteConfig = props.config
return (
<div>
<details open={isOpen}>
<summary>
First text detail.
</summary>
<p>testing</p>
</details>
<details open={isOpen}>
<summary>
Second text detail.
</summary>
<p>testing</p>
</details>
<details open={isOpen}>
<summary>
Third text detail.
</summary>
<p>testing</p>
</details>
<button onClick={() => setIsOpen(true)}>Open All Details.</button>
<button onClick={() => setIsOpen(false)}>Close All Details.</button>
</div>
);
}
ReactDOM.render(<SamplePage/>, document.getElementById("root"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.4/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.4/umd/react-dom.production.min.js"></script>
<div id="root"></div>
答案 1 :(得分:1)
我认为您将对象分解与进口混为一谈。这是您应该做的事情的分解:
// this line imports the react library, which we need in order to
// export a JSX component
const React = require("react");
// this line pulls the useState function out of the React library
// using object destructuring
const { useState } = React;
// it is the same as const useState = React.useState
// we dont need to pass useState in as a prop because its already
// imported in the same file
const SamplePage = props => {
// and here we don't need to call React.useState because it has been
// separated into its own variable
const [isOpen, setIsOpen] = useState(false);
const siteConfig = props.config; // functional components don't use this
return (
<div>
<details open={isOpen}>
<summary>
First text detail.
</summary>
<p>testing</p>
</details>
<details open={isOpen}>
<summary>
Second text detail.
</summary>
<p>testing</p>
</details>
<details open={isOpen}>
<summary>
Third text detail.
</summary>
<p>testing</p>
</details>
<button onClick={() => setIsOpen(false)}>Open All Details.</button>
<button onClick={() => setIsOpen(true)}>Close All Details.</button>
</div>
);
}
module.exports = SamplePage;
让我知道您是否还有其他问题。
编辑
我在函数中添加了props
参数,但我不小心将其忽略了。另外,使用功能组件时,在访问道具时无需使用this
关键字,只需自己调用即可。