React Projectš„Dynamic Accordion in React 19

Thapa Technical
63 followers
3 Video ViewsĀ·Sep 13, 2024
Hello, what's up guys? Today we are going to see Accordion in React version 19.
So guys, what is accordion?
First of all, you can see on the screen.
This is your accordion. We have multiple lists.
You can create as many lists as you want.
First of all, whatever data we are getting here,
we are getting it through the API.
We are getting it through JSON API.
I haven't hardcoded anything.
And second thing, whenever you can see here,
you can see the show button.
Right. And the moment I click on the show button,
boom guys, see.
You can see that we can see our data.
You can see here.
Always, these are all questions.
But as soon as we click on the show button,
we will get the answer.
And along with that, you can see very clearly.
All the buttons are show.
But the one you clicked,
that button has been converted
from show to close.
So from which I am getting to know that
if I want to close here,
I can click on it.
And the moment I click, it comes back to its original state.
So this is called accordion
in normal web design term.
So you can make this accordion
with the help of React.
You can make it with the help of HTML, CSS, Javascript.
Next, View, Angular.
Like with any help,
when you create a functionality
or a layout like this,
then it is called accordion.
Now the best part of this is that
you can add animation to it.
And the second best part is that
suppose I clicked the first one,
I can see it closed
and I can see its output.
So many times it happens that
even if they don't have a proper idea,
when you click the second time,
it remains open.
The second one also opens.
But in our case, it is not like that.
A professional who creates an accordion,
then we make sure
that the moment I open
any other tab,
then the previous tab
closes automatically.
Suppose I click the last one.
I know you can't see the last one.
Let me click the second last one.
I am going to click on this one.
The moment I click,
the previous content,
the second one, automatically closes.
You can see.
At a time, only one will remain open.
And we are going to do this
with the help of pure React.
And you will also learn a special thing
that how to implement
this functionality.
And this functionality
that we are going to implement today,
is used in many places.
Before we start the video,
check out our React version 19 series.
If you haven't seen it yet,
then check it out.
It is the 95th or 96th video.
You won't get any better than this.
And the like target of this video
is only 5000 likes.
And we are very close to 7 lakh subscribers.
If you haven't subscribed yet,
then please do it.
We are going to have a mega project of React.
And I don't want you to miss it.
And the best part,
all the source codes are free.
I have already provided it.
I will provide the link in the description and comment section.
Here is the source code.
And guys, if you want to get my complete
all the videos that I have made
from 2017 to 5 years,
then you can click here.
You will get all my source codes
for only 249 rupees.
So, if you want to support me,
I will be very happy.
Because I don't get anything on YouTube these days.
So, I am depending on you.
So, you can help me.
The source code of this video is free.
So, let's start the video without any delay.
So, you can see that
I have opened a new folder.
We will do it from scratch.
There is no hurry.
Sit comfortably.
So, first of all, let's build our what?
Let me see if there is anything in it.
No, there is nothing.
So, first of all, we have to create
a react application with the help of wit.
mqm create wit
and then latest.
And if you don't know what I have done,
then you need to watch our react series.
React accordian.
I will write accord.
Using react.
And then we will use the javascript.
Now you can see very clearly
that I have to go into that folder.
I have to write npm install and then run it.
We don't need any other package.
So, I go into cd.
Then npm install.
You can see
that I am writing npm install here.
So, I hit enter.
And the moment I do this,
you can see that your project is ready.
We have got the source folder.
And if you would have done it through bun
or pnpm, it would have been a little faster.
But yes, if you write npm install,
it takes a little time.
But our work is done.
So, the moment I do this, my work is done.
Perfect.
Now you can see the end sequence.
So, now
we have to make it like this.
So, what we will do is
first of all, the source folder.
Remove index.css.
Remove app.css.
Because we don't need any default.
Now, I will tell you
that the link of the source code
is given in the description.
Click on it.
Go to source.
We have our api.css.
Copy it completely.
Or you can do it from here.
It's totally up to you.
And paste it here.
So, I have provided the source code.
Now, there is no tension
that you have not shown the css.
Now, I have copied and pasted the source code.
Now, you can see it easily.
What I am doing here.
Alright.
My work is done.
So, you can see
the look and feel of it.
Which is very good.
So, I am removing the index.
Now, I will go to our app.js file.
I have removed it completely.
Alright.
So, let const.
We have app.
And then we need to return.
And I will create a component
by doing echo dn.
I will create a component by doing echo dn.
Export. We have a default.
And then api. There is no need to show you.
I know.
Now, I will go to source folder.
I am going to create a component.
Components. I will create a new file in it.
Now, this accordion which I want to create.
accordion.jsx
Alright.
I have to create all these
in accordion.jsx.
First of all, we have this heading and all.
So, let's go to const.
We have accordion.
Alright.
Now, there will be a lot of things.
First of all,
I will do hello react.
Whether we are getting it or not.
And export it here.
Alright.
I will go here and hit control
and then hit space.
And I have imported it.
So, your project is looking like this.
And I will go here at least.
So, I got hello react.
Alright.
Now, I have to create
something like this.
And one more thing,
in app.jsx,
we have to import
css file.
import.css
Alright. Normal.
If we do this much,
you can see something like this. Perfect.
First of all, instead of hello react,
we need the accordion.
And there are some things
that I want to tell you.
So, first of all,
this part,
as I told you, nothing is hardcoded.
We are getting it through API.
It's good that we are getting it through API.
But,
further,
this particular card
is totally different.
This particular card is different.
What does it mean?
Everyone is different.
Now,
if you see carefully,
first of all,
this yellow box,
has a common component.
Then, this green box,
has a common component.
We have this particular question.
And at the end, we have this particular button.
So, here,
this question and this button,
are in the same common parent.
And with the help of display flags,
one is left,
one is right.
And whenever we click on anyone,
you can see,
we can see the data below.
We have a paragraph here to show it.
I have taken this in a paragraph
and this is my button.
So, if I tell you this,
it's easy user interface.
So, see what I do.
Before making all this,
I will go here.
I will create a new folder on my source folder,
whose name will be api.
OK.
I will create a new folder in it,
whose name will be faq.json.
I have kept it here.
Now, come back here.
I want to make it easy for you.
Here you can see api,
faq.json.
You can copy it here.
You can paste it here.
See, how easy it is.
Now, it's easy for you to follow my code.
It's easy for me already.
So, please like and subscribe.
Now, this data,
I have to get it first.
Then, I have to display it.
The clickable functionality will come later.
First, I have to get this data.
and show it here.
OK. No problem.
So, I will go to the accordion.
How do I get this data?
To get it, I need to import.
I guess, I have written faq.
I will do faq.
Now, watch carefully.
This is also a trick.
That particular path,
and we are in the folder.
Basically, we are in the file.
We have to find out the distance between them.
I mean, I am not talking about math.
The moment I write this,
I am in the api assets component.
We have found out the relative path.
Now, I will go here.
Whenever we play with data in react,
you must know that
we need state.
What do we do?
I will go here.
Set data.
I will write here.
Use state.
By the way, that is an array.
By the way,
I have taken an empty array.
Because, JSON data is an array of an object.
This is an important thing.
Control space.
I am not getting any suggestion.
Here, you have to get
the use state.
From our react library.
There is no problem.
Set data.
I always need data.
No matter what, I always need data.
For that, we will use use effect.
I will go here.
In dependency array,
I have taken an empty array.
This means, only first time,
the data will be loaded.
Here, we have set data.
Here, FAQ.
Our FAQ,
I have passed it here.
Whenever our page
will be loaded for the first time,
because of this use effect,
in our set data,
I am passing this data.
It means,
our JSON data
will be loaded.
If it is loaded,
then it is super easy for us.
We can use map method here.
Let's go over here.
I have some code here.
I want to copy paste it for you.
Because, it is JSX.
But, it is an HTML.
I have to close it.
Closing tag is left.
I will not say unordered.
Now, we have to
loop it one by one.
We have a data.map.
Like this.
Let's have a current
element.
Let's go over here.
We need to return.
Here, I take li.
Like this.
This particular
equation,
it is a complete
data.
I have passed it
left and right.
For both of them,
I am taking a parent div.
It is important to give a class name.
I have given it as
accordion.
That's it.
I have taken a paragraph.
I can't directly
write my question.
We have a current element.
current.
equation.
I will go under this.
We have a button.
I will show it for now.
Alright.
This part is red.
Let's check it out.
Missing key props.
Whenever you use map method,
make sure to pass
key method.
I am giving current element.id.
If I do this,
If I go here,
let's check it out.
We are not getting data here.
I want to refresh it.
The requested module
source API import
does not provide.
Let's do one thing.
We can get that data directly.
By the way.
Wherever I have written faq,
I will make it
small faq.
faq.
I guess there will be no problem.
There is no import and export.
Let's refresh it.
Boom.
My work is done.
I got my data.
So easily.
It's not going to be easy.
I know you are thinking
what have I done.
There was nothing
in it.
Our data
is here.
After updating.
Let me show you.
All your doubts will be cleared.
If I go to component
accordion.
You can see all data is here.
id.
Let me zoom it.
See.
Hooks state.
id.
Question and answer.
All three things
are here.
That's the reason why.
Let me remove it.
CurrentElement.id
CurrentElement.question
and paragraph.
CurrentElement.
We have
answer.
I guess it's answer.
So.
All data is here.
Now I have to
show this data only
when I click on it.
Else I don't have to show.
Now there are things
which you have to keep in mind.
Let's do one thing.
This data.
Let's make a separate component for it.
Because react is all about your component.
I am removing it.
What I am doing.
I am making a folder
on return.
I mean I am making a component.
My key will be
currentElement.id
and
currentData will be equal to
currentElement.
I am passing it.
Let's go here.
I am making a folder
in this component.
It's name will be
SimpleUserInterfaceUI
I am creating a new
faq.jsx component
in that UI.
I am pasting it.
I can't directly
paste this part.
I have to write return.
I don't need this key.
Because
I have pasted it here.
Now this faq.
Control-S-Save
I have pasted it
at the top.
I have imported it.
Now I will copy
this key and current data.
I will come here.
I am directly
destructuring current data.
Now this part equation and answer
I can directly
destructure it.
That is guys
simple.
Let's go with answer.
I am pasting it here.
currentData
At least
I don't need to write these things.
This red warning
is because of
ESLint. I am disabling it.
ESLint
DisableReactPropTypes
Because we are working in TypeScript.
This is done.
At least now you can
check it out.
It's looking nice, clean
and simple.
Look at this.
It's same as before.
Now we have
made it better.
This is done.
I am removing it.
app.csx
I am leaving it.
Now you can see
two things which are very important.
I have written it in
our source code.
Don't worry, I will make it better.
But here is functionality
which you should know.
Look at this.
FunctionToToggleFaq
You have to create a function
named HandleToggle.
You have to pass an id in it.
When you click that button,
this function will be called.
We are passing an id in it.
In that function,
we have to check that
whichever card or tab we clicked,
that id,
that tab,
are we doing it first time or second time.
If we are doing it second time,
it means we have to close it.
If we are doing it first time,
if we click show,
it will open.
If we click again, it will close.
We have to check that.
Second thing,
you have to make sure that
whenever user is clicking
any particular tab,
we will take another state variable
and we will match that state variable's value
with current id.
Or we will set it.
I know it is a bit tricky for you.
But I will explain it to you.
Watch carefully.
Funda is very simple.
Whenever I click on it for the first time,
I have to show the data.
When I click again,
I have to hide the data.
You know this much.
How do they know that
when the first one is open,
when I click on the second one,
how is the first one closing?
There is only one logic behind it.
Every component,
every card has its own unique id.
We will activate ease
and take a state variable
and we will pass the unique id on it.
We are checking on its base.
I will tell you now.
You must have got an idea.
This is our FAQ.
Watch carefully.
Watch it.
I will tell you a simple way.
You will know
why that simple way is not so useful.
I will quickly
activate ease.
I will do one thing.
active id
I will tell you why I am doing this.
We have a use state.
I am putting a false value in it.
It means it is not active.
Watch carefully.
This particular button,
I am going to remove it.
If my button
is active,
see what I am going to do.
If it is active,
which button
do I have to show?
Close button.
If it is not active,
which button do I have to show?
Show button.
Same here.
If it is active,
then and then only
I have to show the answers.
I am saving the controller.
Keep it as it is.
I am coming here.
This is ours.
At least,
earlier,
I was able to see all the answers.
Now, it is not visible.
Why? Because,
by default, it is false.
If it is false,
answers will never be visible.
I will always see the show button.
But, yes, if someone
clicks on this button,
if someone clicks
on this button,
I want to run a function.
handle
button
handle button
I don't want to do anything
in handle button.
I will apply a simple logic.
Whenever someone clicks
on this button,
I will toggle
the active ID.
Whenever
someone clicks on this button,
I will toggle
the active ID value
false
true
individual
click
the moment
show button
button
answer
simple
click
See, if I click again, it will close
So, you will get this data in many places
You will get this kind of accordion
It works fine, but there is one problem
All of them are opened at the same time
See, I have to close them individually
I wanted to click on the first one
And as soon as I click on the last one, it will close
But the first one is there
If I click on the second one, the second one is there
What I mean to say is
When the first one is open
As soon as I click on the second one, it will close
So, at a time, our focus is only on that
But because of this logic, we are not able to do that
So, if you do this, there is no problem
If you do it yourself, it is a big deal
But now I will tell you
Why this method will not work
Because we are not tracking anything
At a time, whom are we clicking
Right, so that is the reason why
What we will do
One more thing
We have a class which I would like to share with you
As soon as
I have a class in the button
If it is active
Okay
So, we will add a class to it
Otherwise, nothing
And if it is active, then I have to add the red color
It is simple
Now, I will tell you where this is active
So, the handle button which I have written here
I will toggle it on
Now, you can say that
I am directly accepting or expecting these props
Okay
And I don't need it here
I just showed you as it is
Even I don't need it here
We will do all this on our parent component
Like this
So, guys, I am passing a new prop here
Is active
And where our active id is
Is it triple equal to our current element
Is it matching with current element's id
Or not
Now, you may think
Active id is false here
So, how can we match false with current id
No, we can't
But
Now, look carefully
I am accepting one thing here
Expect
How do you say expect?
You know
I am expecting that
There is a button called onToggle
Okay
This is also a prop
So, what I do here
OnToggle is a prop
Now, I made it
What I do here
I am passing a function
Okay
On which I handle
Do you remember what I made a while ago
HandleButton
And on this
Our current element
Dot id
I passed
Now
Look
Remember one thing
Whenever
I am clicking on it
So, what are we doing
I am calling this onToggle function
Which we have defined on our parent
And the moment I click on it
It is calling this handleButton function
Okay
So, const handleButton
And we are passing an id here
As parameter
So, I passed id here
Now
Your doubt
Will be cleared now
Why?
Because
Inside this
Set active id
Okay
I am going like this
Simple
And you know
Whenever you use state
Its updated function
We get previous data
So, I am taking previous id
And I am telling you clearly
You have to give a parenthesis like this
Now, I will check
Is my previous id
I mean
Is it triple equal to
Whatever id
User has clicked
If it is equal to that
So, what you do
Add false there
Else
Pass that id there
This
Trick
I won't say trick
This is the main functionality
Which you have to keep in mind
How easy it is
Whenever someone will click on showButton
So, this updated function
We get that
Inside that
I get previous id
And if that previous id
Matches with that id
Which I clicked again
So, you falsify its value
And if previous id
And the one which I clicked
If its id doesn't match
So, leave that id
So, what will happen
Now
The value of active id
Will be the value of current id
And
If the value of current id
Will be here
The value of current id
Like this
So, the one which user clicked
And if it matches with that
Its value will be true
And if it is true
So, this is active
I passed it as a props
So, I will get it here
So, if it is true
So, we have to show it
Okay
I am copying it
I am pasting it here
Like this
Understood
The one which I told you earlier
By falsifying and true
Now, we are checking it with help of this
And I hope now you know
How this is happening
If you do this much
And if I refresh here
So, see
Close
So
So, close and see
See, this is the most important
See, the moment I click on it
It will close
Why?
Because here the same condition is there
First time, see
If I click first time
Okay
And if I click on show
Then I will see this
Why?
Because if I click first time
Then my previous id will be
Its previous id is false
So, will a false
Will be equal to id
Suppose, id is 1
So, false and 1
There is no match
Right
So, what will happen?
This is false
So, its value will be there
So, what we will get here?
An id will be there
So, we are matching that id with this
And obviously, it is matching
So, its value will be true
So, I will get data here
So, because of this
For the very first time
I get data here
Now
Again, I am using the same button
I mean
The same tab
So, when I click this button
It will close
Why?
Because
When I clicked first time
You know that
It has an id
Okay
So, previous id has an id
So, that id
When I will click on it again
So, earlier also it was the same id
So, my previous id and current id
Both are same
So, now its value will be true
So, if it is true
Then it will be false
False means
Its value will be false again
And again it will be false
So, false and id has nothing to do
So, by default, it is false
And if it is false
Then you will neither see this
Nor you will see this
You will not see anything
Are you understanding?
And this is how it works
And yeah
This is it
So, I will say that
Even after explaining so much
If you are getting doubt
So, please repeat this video again
And even if you don't get doubt
Then I can recommend you
110% that you need to practice
Especially
Please watch this part
Where I have explained lifting the state up
Error handling
Passing JSX as a props
Class vs class name
And there are more things
Which I want you to see
React use state
How state works
All these things
Lifting the state up
This part
This is also very important
Please watch this
So, all your doubts will be cleared
So, yeah guys, this is it
I know video was little long
But my main aim was
To explain you
How it works
It will be done with practice
Source code is free
You can buy 249
If you want to support me
People call me patron
If you are giving something
I am giving you 100%
Source code
Like the video
Comment and tell me
Next target is 7 lakh subscribers
If you haven't done it yet
I will meet you in tomorrow's video
I hope you will enjoy
Till then guys
Thank you so much
Take care
