Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,152,569 members, 7,816,397 topics. Date: Friday, 03 May 2024 at 10:39 AM

How Can I Only Select One Component To Use A Function On It (react) with picture - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / How Can I Only Select One Component To Use A Function On It (react) with picture (388 Views)

What Is A Function In Javascript / Can One Developer Build A Function Social App By Himself / Build Your First React With Redux App (2) (3) (4)

(1) (Reply) (Go Down)

How Can I Only Select One Component To Use A Function On It (react) with picture by Fourpockets: 4:16pm On Aug 17, 2022
when i click on the reply button, it affects all the components instead of just one. how can i achieve it to only affect the component i clicked instead of all similar components

note: what im trying to achieve is an interactive comment section

Re: How Can I Only Select One Component To Use A Function On It (react) with picture by Fourpockets: 4:39pm On Aug 17, 2022
.
Re: How Can I Only Select One Component To Use A Function On It (react) with picture by Phantaminum1(f): 6:31pm On Aug 17, 2022
First give all the comments an id, then change the changeCommet to a ternary expression. Like
changeCommet(() => {
return (id === comment.id ? event.target.value : " " )
})
Re: How Can I Only Select One Component To Use A Function On It (react) with picture by Fourpockets: 8:00pm On Aug 17, 2022
Phantaminum1:
First give all the comments an id, then change the changeCommet to a ternary expression. Like
changeCommet(() => {
return (id === comment.id ? event.target.value : " " )
})

Would I have to manually give them IDs, since it's supposed to be a comment section and dynamic

Isn't there a react hook I can use for it
Re: How Can I Only Select One Component To Use A Function On It (react) with picture by chukwuebuka65(m): 11:06pm On Aug 17, 2022
Fourpockets:


Would I have to manually give them IDs, since it's supposed to be a comment section and dynamic

Isn't there a react hook I can use for it

You should add an id property in your comment object. If you are saving your comment to a database,
Every record has a primary key that is unique. You can use that as your id. If you are saving your data in memory , then u have to add the id property manually, may be start from 1 then increment every time you call your addComment function

1 Like

Re: How Can I Only Select One Component To Use A Function On It (react) with picture by Phantaminum1(f): 6:59am On Aug 18, 2022
Fourpockets:


Would I have to manually give them IDs, since it's supposed to be a comment section and dynamic

Isn't there a react hook I can use for it
Yes, there is. useId, but it is only in react18
You can also npm install uuid or nanoid
Re: How Can I Only Select One Component To Use A Function On It (react) with picture by Nobody: 4:56pm On Aug 18, 2022
This is from frontend mentor right.
I’ve done the project myself and yeah, it wasn’t easy.
I ran into a similar challenge and what I did to solve it was to create a Reply component.
And to display the replies for a particular comment, I would map the array containing it’s replies returning that Reply component for each reply, while passing each individual reply(object) into the Reply component as it’s prop.
This way all the local states for each reply would be unique to it (like the state that controls if the reply input is showing or not in this case).
Because the problem here is that you have one state controlling all your reply inputs.

This method should also be the same for the comments. A Comment component that all comments in the comments array will be passed into.
Re: How Can I Only Select One Component To Use A Function On It (react) with picture by Fourpockets: 7:10pm On Aug 18, 2022
Fourpockets:
when i click on the reply button, it affects all the components instead of just one. how can i achieve it to only affect the component i clicked instead of all similar components

note: what im trying to achieve is an interactive comment section


I fixed it, the problem was that I tapped all my components in a usecontext hook and the variable I stored it in was in that hook

1 Like

Re: How Can I Only Select One Component To Use A Function On It (react) with picture by Fourpockets: 7:12pm On Aug 18, 2022
Rgade:
This is from frontend mentor right.
I’ve done the project myself and yeah, it wasn’t easy.
I ran into a similar challenge and what I did to solve it was to create a Reply component.
And to display the replies for a particular comment, I would map the array containing it’s replies returning that Reply component for each reply, while passing each individual reply(object) into the Reply component as it’s prop.
This way all the local states for each reply would be unique to it (like the state that controls if the reply input is showing or not in this case).
Because the problem here is that you have one state controlling all your reply inputs.

This method should also be the same for the comments. A Comment component that all comments in the comments array will be passed into.

Yeah, it's from frontend mentor cool and I've fixed it already
Thanks for the input

1 Like

(1) (Reply)

How Much Does It Cost To Design An Ecommerce App In Nigeria / How Do I Detect AI Content Writing? / X

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 17
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.