Skip to content

Instantly share code, notes, and snippets.

View nluo's full-sized avatar

Nick Luo nluo

View GitHub Profile
This is working as designed. There is a longer discussion about this in #14110 if you're curious.
Let's say for some reason you have AppContext whose value has a theme property, and you want to only re-render some ExpensiveTree on appContextValue.theme changes.
TLDR is that for now, you have three options:
Option 1 (Preferred): Split contexts that don't change together
If we just need appContextValue.theme in many components but appContextValue itself changes too often, we could split ThemeContext from AppContext.
function Button() {
@nluo
nluo / passByReference.js
Last active October 2, 2018 04:58
Pass by reference demo
function changeArr (arr) {
arr.push('another suburb')
}
const suburbs = ['Luctwyche', 'Chemside']
changeArr(suburbs)
console.log('suburbs is ', suburbs)
@nluo
nluo / passByValue.js
Last active October 2, 2018 04:52
Pass by value
function changeNumber (thing) {
thing = thing + 1
}
const myNumber = 123
changeNumber(myNumber)
console.log('my number is ', myNumber)
function changeString (str) {
@nluo
nluo / chunk.js
Created September 2, 2018 08:15
Array Chuck question
// --- Directions
// Given an array and chunk size, divide the array into many subarrays
// where each subarray is of length size
// --- Examples
// chunk([1, 2, 3, 4], 2) --> [[ 1, 2], [3, 4]]
// chunk([1, 2, 3, 4, 5], 2) --> [[ 1, 2], [3, 4], [5]]
// chunk([1, 2, 3, 4, 5, 6, 7, 8], 3) --> [[ 1, 2, 3], [4, 5, 6], [7, 8]]
// chunk([1, 2, 3, 4, 5], 4) --> [[ 1, 2, 3, 4], [5]]
// chunk([1, 2, 3, 4, 5], 10) --> [[ 1, 2, 3, 4, 5]]
@nluo
nluo / redux-typing-example.ts
Last active February 6, 2018 01:08
React redux typings in typescript
import { Dispatch } from 'redux';
import { ThunkAction } from 'redux-thunk';
import { getStore, IState } from './my_store';
let store = getStore();
// Create myThunkAction function with a type of ThunkAction<R, S, E>
let myThunkAction: ThunkAction<Promise<string>, IState, null> =
(dispatch: Dispatch<IState>, getState: () => IState) => {
return new Promise<string>((resolve, reject) => {
@nluo
nluo / .tmux.conf
Created January 20, 2017 06:05
my tmux conf file
# Bind to backtick
unbind C-b
set-option -g prefix `
bind ` send-prefix
set-window-option -g mode-keys vi
# Setup 'v' to begin selection as in Vim
bind-key -t vi-copy v begin-selection
bind -t vi-copy y copy-pipe 'xclip -in -selection clipboard'
@nluo
nluo / useful-docker-commands
Last active February 15, 2017 23:19
Some useful docker commands
### Remove logs from volumes
docker volume rm $(docker volume ls -qf dangling=true)
### Remove unused containers and images
docker rm $(docker ps -q -f 'status=exited')
docker rmi $(docker images -q -f "dangling=true")
### start local postgres, mysql databases
docker run -p 5432:5432 -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD='' -e POSTGRES_DB=abx_modules_test --name "postgres" -d postgres
docker run -p 3306:3306 -e MYSQL_USER=root -e MYSQL_ROOT_PASSWORD='' -e MYSQL_DATABASE=bclconnect_test -e MYSQL_ALLOW_EMPTY_PASSWORD=yes --name "mysql" -d mysql
docker run -p 27017:27017 --name "mongo" -d mongo:3.2
@nluo
nluo / courierpost-sample-error-response.xml
Created November 4, 2015 23:36
CourierPost error response
<?xml version="1.0" encoding="UTF-8"?>
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing">
<s:Header>
<a:Action s:mustUnderstand="1">JobBookingResponse</a:Action>
<a:MessageID>urn:uuid:ed3d1e61-aa7c-490a-a1ee-9d297e938a20</a:MessageID>
</s:Header>
<s:Body>
<ns0:JobBookingResponse xmlns:ns0="http://ECL.Online.BT.JobBooking.JobBookingResponse_v1_5_XML.xsd">
<ns0:RespType>RE</ns0:RespType>
<ns0:MessageID>TMND27049419</ns0:MessageID>