Skip to content

Instantly share code, notes, and snippets.

View ohansemmanuel's full-sized avatar

Ohans Emmanuel ohansemmanuel

View GitHub Profile
@ohansemmanuel
ohansemmanuel / static-data.js
Created May 18, 2018 14:00
Static data generation for Skypey.
const shortid = require("shortid"); // shortid.generate() returns a unique "short" id
const txtgen = require("txtgen"); // txtgen.sentence() returns random "readable" sentences
const faker = require("faker"); // faker is used for generating random fake data.
const _ = require("lodash"); // lodash is a utility lib for Javascript
const users = generateUsers(10);
export const contacts = _.mapKeys(users, "user_id");
export const getMessages = messagesPerUser => {
let messages = {};
_.forEach(users, user => {
- import { PureComponent } from 'react'
+ import { memo } from 'react'

- class Benny extends PureComponent {
-   render () {
  
+ const Benny = memo(() => {
   return <Consumer>
 {position =&gt; }
+ import { PureComponent } from 'react'

- class Benny extends Component {
+ class Benny extends PureComponent {
  render () {
    return <Consumer>
    {position => <svg />}
  </Consumer>
 }
const fetchData = () => {
const stringifyData = data => JSON.stringify(data, null, 2)
const initialData = stringifyData({ data: null })
const loadingData = stringifyData({ data: 'loading...' })
const [data, setData] = useState(initialData)
const [gender, setGender] = useState('female')
const [loading, setLoading] = useState(false)
useEffect(
const App = () => {
const [age, setAge] = useState(99)
const handleClick = () => setAge(age + 1)
const someValue = "someValue"
const doSomething = () => {
return someValue
}
return (
<div>
const initializeState = () => ({
width: 100
})
// ✅ note how the value returned from the fn above overrides initialState below:
const initialState = { width: 15 }
const reducer = (state, action) => {
switch (action) {
case 'plus':
return { width: state.width + 15 }
// 🐢 setState (object merge) vs useState (object replace)
// assume initial state is {name: "Ohans"}
setState({ age: 'unknown' })
// new state object will be
// {name: "Ohans", age: "unknown"}
useStateUpdater({ age: 'unknown' })
// new state object will be
// {age: "unknown"} - initial object is replaced
function TimerWithRefID() {
const setIntervalRef = useRef();
useEffect(() => {
const intervalID = setInterval(() => {
// something to be done every 100ms
}, 100);
// this is where the interval ID is saved in the ref object
setIntervalRef.current = intervalID;
const HoldStringVal = () => {
const textAreaEl = useRef(null);
const stringVal = useRef("This is a string saved via the ref object --- ")
const handleBtnClick = () => {
textAreaEl.current.value =
stringVal.current + "The is the story of your life. You are an human being, and you're on a website about React Hooks";
textAreaEl.current.focus();
};
return (
<section style={{ textAlign: "center" }}>
const AccessDOM = () => {
const textAreaEl = useRef(null);
const handleBtnClick = () => {
textAreaEl.current.value =
"The is the story of your life. You are an human being, and you're on a website about React Hooks";
textAreaEl.current.focus();
};
return (
<section style={{ textAlign: "center" }}>
<div>