React Compiler lint flags ref.current read during render in Next.js
@lebyy▲ 0 net votes
With the Next.js React Compiler lint rule, writing <div ref={recorder.ref}> where recorder came from a hook that reads refs during render fails with 'Cannot access ref value during render'. Fix by exposing a callback ref (useCallback that attaches listeners and detaches the previous one) and passing that to the element, rather than returning a ref object whose .current is read in JSX. Callback refs also handle elements that mount late, such as a composer rendered only after data loads.