Skip to content

Instantly share code, notes, and snippets.

@dnshko
dnshko / .htaccess
Created September 17, 2021 09:51
React cPanel .htaccess file
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule . /index.html [L]
@dnshko
dnshko / Promise.js
Created August 5, 2020 10:47
Promise
const runAsyncFunctions = async () => {
const users = await getUsers()
Promise.all(
users.map(async (user) => {
const userId = await getIdFromUser(user)
console.log(userId)
const capitalizedId = await capitalizeIds(userId)
console.log(capitalizedId)
@dnshko
dnshko / Promise.js
Created August 5, 2020 10:45
Promise
const runAsyncFunctions = async () => {
const users = await getUsers()
for (let user of users) {
const userId = await getIdFromUser(user)
console.log(userId)
const capitalizedId = await capitalizeIds(userId)
console.log(capitalizedId)
}
@dnshko
dnshko / Promise.js
Created August 5, 2020 10:44
Promise
// Third promise relies on the result of the second promise
const capitalizeIds = (id) => {
return new Promise((resolve, reject) => {
return setTimeout(() => resolve(id.toUpperCase()), 200)
})
}
@dnshko
dnshko / Promise.js
Created August 5, 2020 10:43
Promise
// Second promise relies on the result of first promise
const getIdFromUser = (users) => {
return new Promise((resolve, reject) => {
return setTimeout(() => resolve(users.id), 500)
})
}
@dnshko
dnshko / Promise.js
Created August 5, 2020 10:42
Promise
// First promise returns an array after a delay
const getUsers = () => {
return new Promise((resolve, reject) => {
return setTimeout(
() => resolve([{ id: 'jon' }, { id: 'andrey' }, { id: 'tania' }]),
600
)
})
}
@dnshko
dnshko / useEffect.js
Last active August 2, 2020 13:32
useEffect
useEffect(() => {
let cancelRequest = false;
if (!url) return;
const fetchData = async () => {
dispatch({ type: 'FETCHING' });
if (cache.current[url]) {
const data = cache.current[url];
dispatch({ type: 'FETCHED', payload: data });
} else {
@dnshko
dnshko / useReducer.js
Created August 2, 2020 13:28
useReducer
const initialState = {
status: 'idle',
error: null,
data: [],
};
const [state, dispatch] = useReducer((state, action) => {
switch (action.type) {
case 'FETCHING':
return { ...initialState, status: 'fetching' };
@dnshko
dnshko / useRef.js
Created August 2, 2020 13:25
useRef
const useFetch = (url) => {
const cache = useRef({});
const [status, setStatus] = useState('idle');
const [data, setData] = useState([]);
useEffect(() => {
if (!url) return;
const fetchData = async () => {
setStatus('fetching');
if (cache.current[url]) {
@dnshko
dnshko / Fetched Data.js
Created August 2, 2020 13:22
Fetched Data
const cache = {};
const useFetch = (url) => {
const [status, setStatus] = useState('idle');
const [data, setData] = useState([]);
useEffect(() => {
if (!url) return;
const fetchData = async () => {