Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

State is undefined on child component callback #152

Closed
mflash123 opened this issue May 21, 2020 · 10 comments
Closed

State is undefined on child component callback #152

mflash123 opened this issue May 21, 2020 · 10 comments

Comments

@mflash123
Copy link

Parent component:

const [value, setValue] = useState();
...
const someAction = () => {
setValue(123)
}

const emojiHandle = () => {
console.log(value) //display undefined
}
...
...
return(
...
<Emoji callback={handleEmojiChange} /> // -> child component :  onEmojiClick={callback}
<button onClick={someAction}>Press me 1st</button>
)

So, problem is that State is undefined on child component callback.
Reproduction:
Press the button to set state.
Then press on any Emojy to execute callback from parent component and display state.
Callback will show undefined state, which you already set when you press the button.

How to get access to state?

@ealush
Copy link
Owner

ealush commented May 21, 2020

Hey, I am trying to reproduce with your example but I am not sure I am experiencing the bug. Would you mind sharing forking this blitz and sharing what you experience?
https://stackblitz.com/edit/emoji-picker-react-gxa6r9?file=index.js

click fork at the top:
image

@mflash123
Copy link
Author

mflash123 commented May 21, 2020

Hey, I am trying to reproduce with your example but I am not sure I am experiencing the bug. Would you mind sharing forking this blitz and sharing what you experience?
https://stackblitz.com/edit/emoji-picker-react-gxa6r9?file=index.js

click fork at the top:
image

Thanks for reply.
I post a code:
https://stackblitz.com/edit/emoji-picker-react-uxwzsl

UPD
It doesnt depends from child, in a same component, same problem
https://stackblitz.com/edit/react-ck9qgf

@ealush
Copy link
Owner

ealush commented May 28, 2020

I see. this is really weird. I will have to debug this. Thanks for reporting.

@j-edward
Copy link

Is there any update on this?

@jlevy-io
Copy link

jlevy-io commented Aug 9, 2020

I slightly adjusted your code and it seems to work this way. Nothing was being re-rendered so the value was not changing. I added a useEffect that re-renders the App component when value changes

https://stackblitz.com/edit/emoji-picker-react-jakfvz

@LetHergo
Copy link

yes, I also have this problem.
onEmojiClick callBack can not get props value
@ealush

@zpzxgcr
Copy link

zpzxgcr commented Aug 28, 2020

yes, I also have this problem.
onEmojiClick callBack can not get props value
@ealush

@ealush
Copy link
Owner

ealush commented Aug 29, 2020

Hey guys, sorry for taking so long with this.

I believe that the latest version (3.2.3) should solve this issue.

For anyone interested in the details: 2e19179

The picker had two bugs that caused this unexpected behavior.

  1. Stored the consumer's onClick handler in the useReducer hook as an initial value, meaning that it never got refreshed past the initial render - so it always stuck with the first initial scope references. But fixing this was not enough because:
  2. Some of the emoji components are memoized, so they only kept reference to the recent-render handler, which lead to outdated scope variables. Now I store your click handler inside a ref, and always access the function from there.

These two seem to solve the issue, can anyone please post back to see if it works.

@ealush ealush closed this as completed Aug 31, 2020
@paladin952
Copy link

I still face this issue on 4.5.1

@ealush
Copy link
Owner

ealush commented Sep 19, 2023

Hey @paladin952 this is a closed and old issue. Can you please create a new issue with reproduction steps?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants