how to render a component by clicking in reactjs ?
I want to render a component by clicking?
Here's the code
I want to display that language component when I click button from that user.jsx and only render language component and disappear that user component
Language.jsx
User.jsx
js
import React from 'react';
import { User } from './User';
export function App() {
return (
<div>
<User />
</div>
);
}
26 Replies
I can't really unferstand what you are saying but assuming you want to hide the Language comp.when clicling on the button, you can do this:
Inside redirect12:
const [showLang, setShowLang] = useState(true);
In the button:
<button onclick={()=>{setShowLang(false)}}>Next</button>
And for the Language component, render it conditionally:
Oh so I can use ternary operator
To display between the components
Oh I get it
{showLang&&<Language usedetails={usedetails}/>}
{ !showLang ? Input details : <Language /> }
Yeah
Yes exactly
Ok thanks
And if you have many pages, you can also do this in the button:
<button onClick={()=>{setCurrentPage(currentPage+1)}}>Next Page</button>
If I in Language component in browser and I click back , will that state change automatically to false ??
You mean with the back button?
The browser back
In chrome
No, it will go to the previous url
For that you would have to change the url each time you go to a next page
Oh
I see
I get it now
It's a quite a bit more complicated
But I don't understand this
Do u have any articles for this
Its an state function and state variable right ?
UseState
Yeah
You can read more here: https://react.dev/reference/react/useState
useState – React
The library for web and native user interfaces
(About useState)
Alright
I Know useState but I don't understand that what u said about multiple pages and
And increment+ 1
What I meant here is that if in your page you have this:
const [currentPage,setCurrentPage] = useState(1);
return(<div>
{currentPage == 1&& <FirstPage/>}
{currentPage == 2&& <SecondPage/>}{currentPage == 3&& <ThirdPage/>}
</div>)
You can add to yhe currentPage state to change page
So I can change the state to 2,3 by using onClicks
And the pages will be displayed
Yes and then adding one to current value
To go to next page
Nice
I never knew about this
And - 1 for previous
Yep thanks it would help me
But what I said above does not work with browser buttons (the ones on the left of the url)
Keep that in mind
Sure
Yep thanks