Skip to content

Instantly share code, notes, and snippets.

View spencerfeng's full-sized avatar

Spencer Feng spencerfeng

  • Sydney, Australia
View GitHub Profile
@spencerfeng
spencerfeng / socket-context.js
Last active June 14, 2018 12:48
For tutorial: How to use a single instance of Socket.IO in your React app
import React from 'react'
const SocketContext = React.createContext()
export default SocketContext
@spencerfeng
spencerfeng / App.js
Last active June 14, 2018 12:48
For tutorial: How to use a single instance of Socket.IO in your React app
import React
import SocketContext from './socket-context'
import * as io from 'socket.io-client'
const socket = io()
const App = props => (
<SocketContext.Provider value={socket}>
<YourChildComponent />
</SocketContext.Provider>
@spencerfeng
spencerfeng / YourDeeplyNestedComponent.js
Last active August 19, 2018 22:54
For tutorial: How to use a single instance of Socket.IO in your React app
import React, { Component } from 'react'
import SocketContext from './socket-context'
import axios from 'axios'
class YourDeeplyNestedComponent extends Component {
componentDidMount() {
axios.post('/api/messages', {
message: 'This is a new message'
})
.then(response => {
@spencerfeng
spencerfeng / equal_height.js
Created June 14, 2018 12:47
A jQuery plugin that creates equal height columns
$.fn.equalHeight = function(options) {
var maxHeight = 0;
this.each(function(index) {
var height = $(this).height();
if (height > maxHeight) { maxHeight = height };
});
return this.each(function(index) {
@spencerfeng
spencerfeng / PlanetsList.js
Created July 11, 2018 07:19
For tutorial: How to get resources from paginated REST API
import * as Utils from '../utilities'
class PlanetsListTable extends Component {
componentDidMount() {
new Promise((resolve, reject) => {
Utils.getPlanets('https://swapi.co/api/planets', [], resolve, reject)
})
.then(response => {
this.props.loadPlanetsSuccess(response)
})
import moment from 'moment';
const originalDateString = '06/03/2019';
const date = moment(originalDateString, 'DD/MM/YYYY');
//--------------------------------------------------------------
// Convert date string in DD/MM/YYYY format to YYYY-MM-DD format
//--------------------------------------------------------------
const convertedDateString1 = date.format('YYYY-MM-DD');
@spencerfeng
spencerfeng / useCode.ts
Last active August 8, 2020 07:02
This gist is used in this medium article: When React Native Reanimated nodes get evaluated
Animated.useCode(
() => Node | Node[] | boolean | null | undefined,
[...dependencies]
)
@spencerfeng
spencerfeng / MyOpacityComponent.tsx
Last active August 8, 2020 05:32
This gist is used in this medium article: When React Native Reanimated nodes get evaluated
const MyOpacityComponent = () => {
const [toggled, setToggled] = useState(false)
const opacity = useRef<Value<number>>(new Value(0))
useCode(() => [set(opacity.current, runOpacityTimer())], [])
return (
<Animated.View style={{ opacity: opacity.current }}>
<Text>Hello World</Text>
</Animated.View>
@spencerfeng
spencerfeng / use_code_component_mount_example.tsx
Last active August 8, 2020 05:32
This gist is used in this medium article: When React Native Reanimated nodes get evaluated
const UseCodeComponentMountExample = () => {
const value = useRef<Value<number>>(new Value(0))
useCode(() => [debug('console log in useCode', value.current)], [])
return (
<View>Example</View>
)
}
@spencerfeng
spencerfeng / use_code_input_node_updated.tsx
Last active August 8, 2020 05:31
This gist is used in this medium article: When React Native Reanimated nodes get evaluated
const UseCodeInputNodeUpdatedExample = () => {
const [toggled, setToggled] = useState(false)
const destValue = useRef<Value<number>>(new Value(0))
const opacity = useRef<Value<number>>(new Value(0))
useCode(() => [debug('console log in useCode 1', destValue.current)], [])
useCode(() => [debug('console log in useCode 2', opacity.current)], [])
useEffect(() => {