Skip to content

Instantly share code, notes, and snippets.

View austinroy's full-sized avatar

Austin Roy austinroy

  • Nairobi, Kenya
View GitHub Profile

Keybase proof

I hereby claim:

  • I am austinroy on github.
  • I am austinroy (https://keybase.io/austinroy) on keybase.
  • I have a public key ASA5tch3YR4myoY8QjHMD5fHdbltEfrFDsIJ0eq2rLpXxwo

To claim this, I am signing this object:

@austinroy
austinroy / ReactAtellierUsage.js
Created July 23, 2019 11:00
Using react-atellier
import ReactAtellier from 'react-atellier';
import myComponent from 'myComponent';
//import other components here
const componentList = [{
componentName : myComponent.displayName,
component : myComponent
},
//...map other components
];
@austinroy
austinroy / SearchMap.js
Last active July 2, 2019 19:54
Uses react-map-gl to create a searchable map
import "mapbox-gl/dist/mapbox-gl.css"
import "react-map-gl-geocoder/dist/mapbox-gl-geocoder.css"
import React, { Component } from 'react'
import MapGL from "react-map-gl";
import DeckGL, { GeoJsonLayer } from "deck.gl";
import Geocoder from "react-map-gl-geocoder";
const token = process.env.REACT_APP_TOKEN
class SearchableMap extends Component {
@austinroy
austinroy / GeolocateMap.js
Last active July 2, 2019 19:52
Uses react-map-gl to find a user's location
import React,{ useState } from 'react'
import MapGL, {GeolocateControl } from 'react-map-gl'
import config from '../config'
import 'mapbox-gl/dist/mapbox-gl.css'
const TOKEN=config.REACT_APP_TOKEN
const geolocateStyle = {
float: 'left',
margin: '50px',
@austinroy
austinroy / getSnapshotBeforeUpdateExample.js
Created May 28, 2019 20:14
An example of a use case for the getSnapshotBeforeUpdate lifecycle method in React
class ScrollingList extends React.Component {
listRef = null;
getSnapshotBeforeUpdate(prevProps, prevState) {
// Are we adding new items to the list?
// Capture the scroll position so we can adjust scroll later.
if (prevProps.list.length < this.props.list.length) {
return (
this.listRef.scrollHeight - this.listRef.scrollTop
);
@austinroy
austinroy / getDerivedStateFromPropsExample.js
Created May 28, 2019 15:54
Example of getDerivedStateFromProps usage
state = { cachedSomeProp: null };
static getDerivedStateFromProps(nextProps, prevState) {
// do things with nextProps.someProp and prevState.cachedSomeProp
return {
cachedSomeProp: nextProps.someProp,
..
};
}
@austinroy
austinroy / SupsenseWithCache.js
Created May 17, 2019 09:01
Cached Supsense with react-cache
const getInfo = () => fetch("https://myApi.apiexample").then(res => res.json())
const ApiResource = createResource(getInfo)
const SayHello = () => {
const data = ApiResource.read()
return <div>Hi {data.name}</div>
}
@austinroy
austinroy / Suspense.js
Created May 17, 2019 08:26
Example of Suspense in React
const DataComponent = React.lazy(() => import('./DataComponent'));
function MySuspenseComponent(){
return (
<Suspense fallback={<Spinner />}>
<DataComponnent/>
<Suspense>
)
}
@austinroy
austinroy / Suspense.js
Created May 17, 2019 08:26
Example of Suspense in React
const DataComponent = React.lazy(() => import('./DataComponent'));
function MySuspenseComponent(){
return (
<Suspense fallback={<Spinner />}>
<DataComponnent/>
<Suspense>
)
}
// ------------
// First render
// ------------
useState('Mary') // 1. Initialize the name state variable with 'Mary'
useEffect(persistForm) // 2. Add an effect for persisting the form
useState('Poppins') // 3. Initialize the surname state variable with 'Poppins'
useEffect(updateTitle) // 4. Add an effect for updating the title
// -------------
// Second render
// -------------