How do I hide my page “section” when I click on a button. and show it another button is clicked Here’s my code
JavaScript
x
29
29
1
import './App.css';
2
import Typed from 'react-typed';
3
import { useState, useEffect } from 'react';
4
function App() {
5
return (
6
<div className='text-white'>
7
<div id='index' className='index max-w-[800px] mt-[-96px] w-full h-screen mx-auto text-center flex flex-col justify-center'>
8
<div className='flex justify-center items-center'>
9
<h1 className='md:text-7xl sm:text-6xl text-4xl font-bold md:py-6'>Roll</h1>
10
<Typed
11
className="md:text-7xl sm:text-6xl text-4xl font-bold md:py-6 text-gray-400"
12
strings={['simply', 'design']}
13
typeSpeed={70}
14
backSpeed={100}
15
loop/>
16
</div>
17
<p className='md:text-3xl sm:text-2xl text-xl font-bold py-4 '>best <a className='underline text-gray-300'>design</a> and <a className='underline text-gray-300'>simplistic.</a></p>
18
<button className='bg-white text-black w-[200px] transition-[0.5s] rounded-lg font-bold my-6 mx-auto py-3 ring-2 ring-gray-300 hover:bg-slate-300 hover:shadow-xl hover:shadow-white hover:scale-110 '>Create</button>//clicking this button will show the div below which has the id of info and hide this current div
19
</div>
20
<div id='info' className='info max-w-[800px] mt-[-96px] w-full h-screen mx-auto text-center flex flex-col justify-center'>
21
<h1>Hello</h1>
22
</div>
23
<div id='ino' className='info max-w-[800px] mt-[-96px] w-full h-screen mx-auto text-center flex flex-col justify-center'>
24
<h1>Hello</h1>
25
</div>
26
</div>
27
);
28
}
29
how do i make the onClick event work it out?
Advertisement
Answer
Just set a state
let’s call it for example hide
and set default value false
, and on the button click turn it to true
.
And you can conditionally hide the button
section and show the info
section.
JavaScript
1
54
54
1
import "./App.css";
2
import Typed from "react-typed";
3
import { useState, useEffect } from "react";
4
function App() {
5
const [hide, setHide] = useState(false);
6
return (
7
<div className="text-white">
8
{!hide ? (
9
<div
10
id="index"
11
className="index max-w-[800px] mt-[-96px] w-full h-screen mx-auto text-center flex flex-col justify-center"
12
>
13
<div className="flex justify-center items-center">
14
<h1 className="md:text-7xl sm:text-6xl text-4xl font-bold md:py-6">
15
Roll
16
</h1>
17
<Typed
18
className="md:text-7xl sm:text-6xl text-4xl font-bold md:py-6 text-gray-400"
19
strings={["simply", "design"]}
20
typeSpeed={70}
21
backSpeed={100}
22
loop
23
/>
24
</div>
25
<p className="md:text-3xl sm:text-2xl text-xl font-bold py-4 ">
26
best <a className="underline text-gray-300">design</a> and{" "}
27
<a className="underline text-gray-300">simplistic.</a>
28
</p>
29
<button
30
className="bg-white text-black w-[200px] transition-[0.5s] rounded-lg font-bold my-6 mx-auto py-3 ring-2 ring-gray-300 hover:bg-slate-300 hover:shadow-xl hover:shadow-white hover:scale-110"
31
onClick={() => setHide(true)}
32
>
33
Create
34
</button>
35
//clicking this button will show the div below which has the id of
36
info and hide this current div
37
</div>
38
) : (
39
<div
40
id="info"
41
className="info max-w-[800px] mt-[-96px] w-full h-screen mx-auto text-center flex flex-col justify-center"
42
>
43
<h1>Hello</h1>
44
</div>
45
)}
46
<div
47
id="ino"
48
className="info max-w-[800px] mt-[-96px] w-full h-screen mx-auto text-center flex flex-col justify-center"
49
>
50
<h1>Hello</h1>
51
</div>
52
</div>
53
);
54
}