Skip to content

Warnings when assigning to ref in render phase? #14935

@jamesknelson

Description

@jamesknelson

Do you want to request a feature or report a bug?

Feature

What is the current behavior?

There are no warnings when assigning to ref.current in render phase.

What is the expected behavior?

Given that this mostly works now, but may cause issues on release of concurrent mode, it would be helpful to have a warning when assigning to a prop outside of useEffect().

E.g. I recently wrote this code, which works fine at the moment, and didn't realize the possible issue until @gaearon pointed out the missing useEffect():

export function useDidChange(value) {
  let ref = useRef()
  let didChange = ref.current !== value
  ref.current = value
  return didChange
}

If a warning is not possible due to the lazy initialization pattern, perhaps an eslint rule may be a solution?

Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?

All versions

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions