I'm working with Docusaurus, which provides a siteConfig.js
as a config props. As such, I have to use this props to build my site components. Working code is formatted like this:
const React = require("react");
class SamplePage extends React.Component {
render() {
const siteConfig = this.props.config;
return <div>{siteConfig.title}</div>;
}
}
module.exports = SamplePage;
I have another segment of working code shown in this question, but it uses a different set-up where const {useState} = React;
is used in place of const React = require("react");
and <div id="root">
with ReactDOM.render(<SamplePage/>, document.getElementById("root"));
in place of module.exports = SamplePage;
. I understand this allows running code snippets on SE, but it doesn't show me how the imports and exports are supposed to function in the context of this Docusaurus project. What I want to do is incorporate the code segment into the React.Component
or otherwise construct this component to employ theuseState
hook with the config props in order to assert or deny the isOpen
attribute of 3 detail
tags, using 2 button
(s) to control the hook:
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"));
For the code snippet:
<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>
My question is how to combine these code segments. I've tried to construct this component in many different ways, but I can't get the button
(s) to fire the onClick()
effect. For example, I tried:
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;
This throws an "invalid hook call", of course, since I can't actually use the useState
hook in my current set-up. I get unexpected tokens and reference errors in all my other constructions.