Coding⏱️ 2 min read📅 2026-05-29

How to Fix: ESLint "Cannot access refs during render" error but I am not acce

ESLint error on accessing ref during render in React component.

Quick Answer: The issue is likely due to the fact that you're using a custom hook with a ref, but not actually using the ref's current value. Try destructuring the hook's return value instead of accessing the ref directly.

To resolve the ESLint error, you need to understand that the rule is intended to prevent accidental access to refs during render. Since you are not accessing `ref.current` directly in your code, the issue might be related to how you're passing the ref to the component.

🛑 Root Causes of the Error

  • Passing refs as props without checking their type or nullability.

✅ Best Solutions to Fix It

Method 1: Check Ref Type and Nullability

  1. Step 1: Ensure that the ref you're passing is of type `React.RefObject` or `React.Ref`. You can do this by checking the type in your component.

Method 2: Use Optional Chaining

  1. Step 1: In your code, replace `dialog.ref` with `${dialog?.ref}`. This will ensure that you're not trying to access the ref if it's null or undefined.

💡 Conclusion

By following these methods, you should be able to resolve the ESLint error and avoid accidental access to refs during render.

Did this fix your problem?

If not, try searching for specific error codes.

🔍 Search Error Database

❓ Frequently Asked Questions