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

Add getValue method for getting value when we need inside an event #27928

Closed
wants to merge 1 commit into from
Closed

Add getValue method for getting value when we need inside an event #27928

wants to merge 1 commit into from

Conversation

behnammodi
Copy link
Contributor

@behnammodi behnammodi commented Aug 23, 2021

handleChange just redefines when onChange change

Also, we can use useStateRef where we need state's value inside an event that we won't redefines by useCallback

@mui-pr-bot
Copy link

mui-pr-bot commented Aug 23, 2021

Details of bundle changes (experimental)

Generated by 🚫 dangerJS against bd032f7

@behnammodi behnammodi changed the title add getValue method for getting value when we need inside an event WIP: add getValue method for getting value when we need inside an event Aug 23, 2021
@behnammodi behnammodi changed the title WIP: add getValue method for getting value when we need inside an event Add getValue method for getting value when we need inside an event Aug 23, 2021
Copy link
Member

@eps1lon eps1lon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you expand a bit with concrete examples why this change is needed?

The pattern introduced in useStateRef is not safe in concurrent rendering and React 18 is now in sight. I suspect that there's a problem you have with React in general so it might be better to open a React RFC. Because right now, I can't see how this is a Material-UI issue.

@eps1lon eps1lon added new feature New feature or request status: waiting for author Issue with insufficient information labels Aug 24, 2021
@behnammodi
Copy link
Contributor Author

Could you expand a bit with concrete examples why this change is needed?

The pattern introduced in useStateRef is not safe in concurrent rendering and React 18 is now in sight. I suspect that there's a problem you have with React in general so it might be better to open a React RFC. Because right now, I can't see how this is a Material-UI issue.

Thanks for the review, this is not an issue, this is an improvement for reducing execution of useCallback, useMemo and also useEffect,...

ex:

const [count, setCount] = useState();

useEffect(() => {
    const handleVisibiltuy = () => {
      console.log(count)
    }

   document.addEventListener('visibilitychange', handleVisibiltuy);
   
   return () =>  document.removeEventListener('visibilitychange', handleVisibiltuy);
}, [count]);

or:

const [count, setCount, getCount] = useStateRef();

useEffect(() => {
    const handleVisibiltuy = () => {
      console.log(getCount())
    }

   document.addEventListener('visibilitychange', handleVisibiltuy);
   
   return () =>  document.removeEventListener('visibilitychange', handleVisibiltuy);
}, []);

Which one is better you think?

Also your point is can be good, I will open a RFC inside React

@eps1lon
Copy link
Member

eps1lon commented Aug 24, 2021

Which one is better you think?

The first one since it's safe with concurrent rendering.

Though it's clear now that this is an enhancement you'd wish to see in React. We are not equipped to run such experiments.

There's already a discussion about this topic on the React repo: facebook/react#14543

@eps1lon eps1lon closed this Aug 24, 2021
@behnammodi
Copy link
Contributor Author

All right, I opened another facebook/react#22163

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new feature New feature or request status: waiting for author Issue with insufficient information
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants