r/learnreactjs • • Jul 30 '22

Help me fix my button

I try to create user-management according to this repo

Everything went great, I add couple more from to the edit/ add user modal.

But the edit button need to be click twice in order to show edit modal (first click to change 'editing' state to true, second to show the modal) Also when I close the edit user model and try to open add user modal without click update, it will show edit user modal instead of add user modal.

I know there is something to do with condition of some states and functions, but I don't know lol

please help

Thanks

p.s. my code is almost the same as in the repo, please take a look on my code here:

https://github.com/MangeshIpper/dataminrapp/blob/master/src/components/Menu.tsx

add user model
edit user model
1 Upvotes

11 comments sorted by

View all comments

Show parent comment

1

u/toppyc4 Aug 01 '22

Thankyou for your effort

I've watch some tutorials, but to apply them in this project is kinda confusing

please take a look at my code (in user-management/src/components/Menu.tsx)

thanks again

1

u/Kablaow Aug 01 '22 edited Aug 01 '22

something like this seems to work:

const onEdit = (newUser: any) => {
setEdit(true)
setNewUser({...newUser, newUser})
/* console.log("editing", editing)
if(editing == true) {
  handleShow()
} */
console.log("onEdit " +editing)
}
useEffect(() => { if (editing && !!newUser) { handleShow(); } }, [editing, newUser])

let me know if you need help understanding it. It's not a perfect solution perhaps but it works. You might also want to handle the closing of the module in the same useEffect, so when editing is false you reset the user.

1

u/toppyc4 Aug 02 '22

Thank you bro

if you have time to explain my code for me 🤣 would be great