Skip to content

Commit

Permalink
Update react usage to custom hook
Browse files Browse the repository at this point in the history
  • Loading branch information
devbymak committed Apr 23, 2024
1 parent a9f90cc commit f7c3b5e
Showing 1 changed file with 24 additions and 14 deletions.
38 changes: 24 additions & 14 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,33 @@ npm install devtools-detect

## React Usage
```jsx
import { useState } from 'react';
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;
}
```
```jsx
import {useDevToolsStatus} from './useDevToolsStatus.js';

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

if (window) {
window.addEventListener('devtoolschange', function (e) {
if (e.detail.isOpen) {
setIsDevToolsOpen(true);
} else {
setIsDevToolsOpen(false);
}
});
}

return isDevToolsOpen ? 'OPEN' : 'CLOSE';
const isDevToolsOpen = useDevToolsStatus();
return isDevToolsOpen ? 'OPEN' : 'CLOSED';
}
```

Expand Down

0 comments on commit f7c3b5e

Please sign in to comment.