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 React usage #70

Merged
merged 3 commits into from
Apr 23, 2024
Merged

Add React usage #70

merged 3 commits into from
Apr 23, 2024

Conversation

devbymak
Copy link
Contributor

No description provided.

@sindresorhus
Copy link
Owner

I think it would be better to make it more reusable, for better readability. Can you double-check that this is correct?


import {useState, useEffect} from 'react';
import devtoolsDetect from 'devtools-detect';

export function useDevToolsStatus() {
	const [isDevToolsOpen, setIsDevToolsOpen] = useState(devtoolsDetect.isOpen);

	useEffect(() => {
		const handleChange = event => {
			setIsDevToolsOpen(event.detail.isOpen);
		};

		window.addEventListener('devtoolschange', handleChange);

		return () => {
			window.removeEventListener('devtoolschange', handleChange);
		};
	}, []);

	return isDevToolsOpen;
}
import {useDevToolsStatus} from './useDevToolsStatus.js';

export default function App() {
	const isDevToolsOpen = useDevToolsStatus();
	return isDevToolsOpen ? 'OPEN' : 'CLOSED';
}

@devbymak
Copy link
Contributor Author

sure you're right, I tried to explain it more simply, actually the better way is to use it as a custom hook.
your custom hook works correctly.

@sindresorhus sindresorhus merged commit 38ab02f into sindresorhus:main Apr 23, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants