Skip to content

Instantly share code, notes, and snippets.

View adamkleingit's full-sized avatar

Adam Klein adamkleingit

View GitHub Profile
@adamkleingit
adamkleingit / virtualScroll.js
Last active October 19, 2023 07:47
Vanilla Virtual Scroll Pseudo Code
const VirtualScroll = ({
renderItem,
itemCount,
viewportHeight,
rowHeight,
nodePadding,
}) => {
const totalContentHeight = itemCount * rowHeight;
let startNode = Math.floor(scrollTop / rowHeight) - nodePadding;
@adamkleingit
adamkleingit / CompsUsingLocalization.js
Created July 9, 2019 10:44
Using ReusableLoclization
import {useCurrentLocale, LocaleSwitcher} from 'reusable-locale';
const Comp1 = () => {
const currentLocale = useCurrentLocale(); // Using a global store from external lib
}
const Comp2 = () => {
return <LocaleSwitcher/>; // External components using the same global store
}
@adamkleingit
adamkleingit / ReusableSelectors.js
Created July 9, 2019 10:31
Reusable Selectors
import {createStore} from 'reusable';
const useTodos = createStore(() => useState([]));
const Comp = () => {
const todosCount = useTodos(
([todos]) => todos.length
);
...
};
@adamkleingit
adamkleingit / App.jsx
Created July 9, 2019 10:29
App with ReusableProvider
import {ReusableProvider} from 'reusable';
export const App = () => (
<ReusableProvider>
{ /* App Components */ }
</ReusableProvider>
);
import {useCurrentUser} from '../stores/currentUser.store';
const Comp1 = () => {
const { user } = useCurrentUser();
...
}
const Comp2 = () => {
const { user } = useCurrentUser(); // Same user
...
@adamkleingit
adamkleingit / simple.stores.js
Created July 9, 2019 10:19
Simple Reusable Stores Example
import {createStore} from 'reusable';
export const useLocale = createStore(() => useState('en'));
export const useSomething = createStore(() => useReducer(...));
import {createStore} from 'reusable';
export const useCurrentUser = createStore(() => {
const [user, setUser] = useState({...});
...
return {
user,
...
}
});
@adamkleingit
adamkleingit / redux-hooks.js
Last active November 8, 2018 06:32
Experimenting with Hooks and Redux
const useStore = () => {
const { store } = useContext(ReactReduxContext);
return store;
};
const useSelector = selector => {
const store = useStore();
const prevState = selector(store.getState());
"Redux forces you to write good code" - I've heard that sentence many times.
In fact - it's quite easy to write bad code with Redux, as I've seen many times.
In this talk I will show some bad practices and techniques with Redux, and how to avoid them.
@adamkleingit
adamkleingit / package-lock.json
Last active February 20, 2018 08:12
Files for deploying Node.JS to Heroku
(automatically generated from npm)