반응형
Collection of sexy React Hooks ready to install with NPM
- useTitle
- useInput
- usePageLeave
- useClick
- useFadeIn
- useFullscreen
- useHover
- useNetwork
- useNotification
- useScroll
- useTabs
- usePreventLeave
- useConfirm
- useAxios
1. useInput
useInput은 기본적으로 Input을 업데이트 한다.
export const useInput = (initialValue, validator) => {
const [value, setValue] = useState(initialValue);
const onChange = (event) => {
const {
target: { value },
} = event;
let willUpdate = true;
if (typeof validator === 'function') {
willUpdate = validator(value);
}
if (willUpdate) {
setValue(value);
}
};
return { value, onChange };
};
2. useTabs
import React, { useState } from 'react';
import ReactDOM from 'react-dom';
const content = [
{
tab: 'Section 1',
content: "I'm the content of the Section 1",
},
{
tab: 'Section 2',
content: "I'm the content of the Section 2",
},
];
const useTabs = (initialTab, allTabs) => {
const [currentIndex, setCurrentIndex] = useState(initialTab);
return {
currentItem: allTabs[currentIndex],
changeItem: setCurrentIndex,
};
};
const App = () => {
const { currentItem, changeItem } = useTabs(0, content);
return (
<div className='App'>
{content.map((section, index) => (
<button onClick={() => changeItem(index)}>{section.tab}</button>
))}
<div>{currentItem.content}</div>
</div>
);
};
3. useTitle
import React, { useState, useEffect } from 'react';
import ReactDOM from 'react-dom';
const useTitle = (initialTitle) => {
const [title, setTitle] = useState(initialTitle);
const updateTitle = () => {
const htmlTitle = document.querySelector('title');
htmlTitle.innerText = title;
};
useEffect(updateTitle, [title]);
return setTitle;
};
const App = () => {
const titleUpdater = useTitle('Loading...');
setTimeout(() => titleUpdater('Home'), 3000);
return (
<div className='App'>
<div>Hi</div>
</div>
);
};
4. useClick
import React, { useEffect, useRef } from 'react';
import ReactDOM from 'react-dom';
const useClick = (onClick) => {
const element = useRef();
useEffect(() => {
if (element.current) {
element.current.addEventListener('click', onClick);
}
return () => {
if (element.current) {
element.current.removeEventListener('click', onClick);
}
};
}, []);
return element;
};
const App = () => {
const sayHello = () => console.log('say hello');
const title = useClick(sayHello);
return (
<div className='App'>
<h1 ref={title}>Hi</h1>
</div>
);
};
5. useConfirm
import React from 'react';
import ReactDOM from 'react-dom';
const useConfirm = (message = '', callback, rejection) => {
if (typeof callback !== 'function') {
return;
}
const confirmAction = () => {
if (window.confirm(message)) {
callback();
} else {
rejection();
}
};
return confirmAction;
};
const App = () => {
const deleteWorld = () => console.log('Deleting the world...');
const abort = () => console.log('Aborted');
const confirmDelete = useConfirm('Are you sure?', deleteWorld, abort);
return (
<div className='App'>
<button onClick={confirmDelete}>Delete the world</button>
</div>
);
};
6. usePreventLeave
import React, { useEffect } from 'react';
import ReactDOM from 'react-dom';
const usePreventLeave = () => {
const listener = (event) => {
event.preventDefault();
event.returnValue = '';
};
const enablePrevent = () => window.addEventListener('beforeunload', listener);
const disablePrevent = () => window.removeEventListener('beforeunload', listener);
return { enablePrevent, disablePrevent };
};
const App = () => {
return (
<div className='App'>
<button onClick={enablePrevent}>Protect</button>
<button onClick={disablePrevent}>Unprotect</button>
</div>
);
};
7. useBeforeLeave
import React, { useEffect } from 'react';
import ReactDOM from 'react-dom';
const useBeforeLeave = (onBefore) => {
const handle = (event) => {
const { clientY } = event;
if (clientY <= 0) {
onBefore();
}
};
useEffect(() => {
document.addEventListener('mouseleave', handle);
return () => document.removeEventListener('mouseleave', handle);
}, []);
};
const App = () => {
const begForLife = () => console.log('Pls dont leave');
useBeforeLeave(begForLife);
return (
<div className='App'>
<h1>Hello</h1>
</div>
);
};
8. useFadeIn
import React, { useRef, useEffect } from 'react';
import ReactDOM from 'react-dom';
const useFadeIn = (duration = 1, delay = 0) => {
const element = useRef();
useEffect(() => {
if (element.current) {
const { current } = element;
current.style.transition = `opacity ${duration}s ease-in-out ${delay}s`;
current.style.opacity = 1;
}
}, []);
return { ref: element, style: { opacity: 0 } };
};
const App = () => {
const fadeInH1 = useFadeIn(1, 2);
const fadeInP = useFadeIn(5, 10);
return (
<div className='App'>
<h1 {...fadeInH1}>Hello</h1>
<p {...fadeInP}>lorem ipsum lalalalalala</p>
</div>
);
};
9. useNetwork
import React, { useState, useEffect } from 'react';
import ReactDOM from 'react-dom';
const useNetwork = (onChange) => {
const [status, setStatus] = useState(navigator.onLine);
const handleChange = () => {
setStatus(navigator.onLine);
};
useEffect(() => {
window.addEventListener('online', handleChange);
window.addEventListener('offline', handleChange);
return () => {
window.removeEventListener('online', handleChange);
window.removeEventListener('offline', handleChange);
};
}, []);
return status;
};
const App = () => {
const online = useNetwork();
return (
<div className='App'>
<h1>{online ? 'Online' : 'Offline'}</h1>
</div>
);
};
10. useScroll
import React, { useState, useEffect } from 'react';
import ReactDOM from 'react-dom';
const useScroll = () => {
const [state, setState] = useState({
x: 0,
y: 0,
});
const onScroll = () => {
setState({ y: window.scrollY, x: window.scrollX });
};
useEffect(() => {
window.addEventListener('scroll', onScroll);
return () => {
window.removeEventListener('scroll', onScroll);
};
}, []);
return state;
};
const App = () => {
const { y } = useScroll();
return (
<div className='App' style={{ height: '1000vh' }}>
<h1 style={{ position: 'fixed', color: y > 100 ? 'red' : 'blue' }}>Hi</h1>
</div>
);
};
반응형
'JavaScript > React' 카테고리의 다른 글
[React] 라우팅(Routing)이란? (0) | 2021.11.24 |
---|---|
[React] React란? (0) | 2021.11.18 |
[React] State & Event (0) | 2021.11.18 |
[React] Props & Event (0) | 2021.11.18 |
[React] map 함수 적용시 key props를 부여하는 이유 (0) | 2021.11.18 |
댓글