What Happens Without the 'use' Prefix
You'll learn: Why skipping the 'use' prefix breaks React's safety checks and can lead to subtle bugs.
The Safety Net Disappears
Remember how we established that custom hooks must start with 'use'? This isn't just a suggestion—it's how React's linting tools identify hooks and enforce the Rules of Hooks.
The eslint-plugin-react-hooks plugin specifically looks for functions starting with 'use' to verify they follow the rules. Without that prefix, the linter thinks your function is a regular utility function and doesn't check it at all.
What Goes Wrong
Imagine you create a custom hook but name it getUser() instead of useUser():
function getUser(userId) {
const [user, setUser] = useState(null); // Hook call inside!
useEffect(() => {
fetchUser(userId).then(setUser);
}, [userId]);
return user;
}
The linter won't complain. It sees getUser and thinks "that's just a regular function." But you're calling hooks inside, which means it must follow the Rules of Hooks.
Now someone uses it conditionally:
function Profile({ userId }) {
if (userId) {
const user = getUser(userId); // Conditional hook call!
// ...
}
}
Still no lint error. The linter doesn't know getUser contains hooks. But at runtime, React's hook order gets violated when userId changes from falsy to truthy, potentially causing crashes or state corruption.
The 'use' Prefix as a Signal
The 'use' prefix is a machine-readable contract: "This function contains hooks, check it accordingly." Without it, you lose automated protection against breaking the Rules of Hooks.
Key Takeaway: Without the 'use' prefix, eslint-plugin-react-hooks cannot enforce the Rules of Hooks, leaving you vulnerable to runtime errors that would otherwise be caught during development.