Skip to content

Instantly share code, notes, and snippets.

View quisido's full-sized avatar
🌸
trying my best

quisi.do quisido

🌸
trying my best
View GitHub Profile
@quisido
quisido / global-reducer.js
Created December 3, 2018 15:07
Manage global state with React Hooks
setGlobal({ count: 0 });
function reducer(state, action) {
switch (action.type) {
case 'reset':
return { count: 0 };
case 'increment':
return { count: state.count + 1 };
case 'decrement':
return { count: state.count - 1 };
@quisido
quisido / global-reducer.js
Created December 3, 2018 15:06
Manage global state with React Hooks
const initialState = { count: 0 };
function reducer(state, action) {
switch (action.type) {
case 'reset':
return initialState;
case 'increment':
return { count: state.count + 1 };
case 'decrement':
return { count: state.count - 1 };
@quisido
quisido / use-global-mini.js
Created December 3, 2018 15:05
Manage global state with React Hooks
const MyComponent = () => {
const [ global, setGlobal ] = useGlobal();
if (global.x) {
return global.x;
}
return global.y;
};
@quisido
quisido / how2use-use-global.js
Created December 3, 2018 15:05
Manage global state with React Hooks
const MyComponent = () => {
const [ global, setGlobal ] = useGlobal();
return (
<img
alt="Avatar"
onClick={() => {
const newAvatar = prompt("Enter your avatar URL:");
setGlobal({
avatar: newAvatar
});
@quisido
quisido / use-global.js
Created December 3, 2018 15:04
Manage global state with React Hooks
import { useGlobal } from 'reactn';
const MyComponent = () => {
const [ avatar, setAvatar ] = useGlobal('avatar');
return (
<img
alt="Avatar"
onClick={() => {
const newAvatar = prompt("Enter your avatar URL:");
setAvatar(newAvatar);
@quisido
quisido / set-global.js
Created December 3, 2018 15:00
Manage global state with React Hooks
import { setGlobal } from 'reactn';
setGlobal({
avatar: 'anonymous.png'
});
@quisido
quisido / use-state-example.js
Created December 3, 2018 14:58
Manage global state with React Hooks
import { useState } from 'react';
const MyComponent = () => {
const [ avatar, setAvatar ] = useState('anonymous.png');
return (
<img
alt="Avatar"
onClick={() => {
const newAvatar = prompt("Enter your avatar URL:");
setAvatar(newAvatar);
@quisido
quisido / use-state.js
Created December 3, 2018 14:58
Manage global state with React Hooks
const [ value, setValue ] = useState(DEFAULT_VALUE);
@quisido
quisido / final.js
Last active April 17, 2019 07:14
Implementing Quicksort in JavaScript
const quickSort = (
unsortedArray,
comparator = defaultComparator
) => {
// Create a sortable array to return.
const sortedArray = [ ...unsortedArray ];
// Recursively sort sub-arrays.
const recursiveSort = (start, end) => {
@quisido
quisido / setup.js
Last active April 1, 2019 01:24
Implementing Quicksort in JavaScript
const quickSort = (
unsortedArray,
comparator = defaultComparator
) => {
// Create a sortable array to return.
const sortedArray = [...unsortedArray];
// Recursively sort sub-arrays.
const recursiveSort = (start, end) => {