Skip to content

Instantly share code, notes, and snippets.

View oshell's full-sized avatar
🎯
Focusing

oshell oshell

🎯
Focusing
View GitHub Profile
@oshell
oshell / App.jsx
Created March 19, 2020 18:06
Mobx TodoList Example
import React from 'react'
import ReactDOM from 'react-dom'
import { observer, useObservable } from 'mobx-react-lite'
import TodoList from './components/TodoList'
import Footer from './components/Footer'
import './styles.css'
export const App = observer(() => {
const store = useObservable({
@oshell
oshell / component.jsx
Created March 19, 2020 17:46
Pullstate mutate global state example
return (
<div
style={{
background: isDarkMode ? "black" : "white",
color: isDarkMode ? "white" : "black",
}}>
<h1>Hello Pullstate</h1>
<button
onClick={() =>
UIStore.update(s => {
@oshell
oshell / component.jsx
Created March 19, 2020 17:44
pullstate read store example
import * as React from "react";
import { UIStore } from "./UIStore";
export const App = () => {
const isDarkMode = UIStore.useState(s => s.isDarkMode);
return (
<div
style={{
background: isDarkMode ? "black" : "white",
@oshell
oshell / main.js
Created March 19, 2020 17:42
initialize Global PullState
import { Store } from "pullstate";
export const UIStore = new Store({
isDarkMode: true,
});
@oshell
oshell / main.js
Created March 19, 2020 17:40
Initialize Reactn Global State
import React, { setGlobal } from 'reactn';
import ReactDOM from 'react-dom';
import App from './App';
// Set an initial global state directly:
setGlobal({
cards: [],
disabled: false,
initial: 'values',
x: 1,
@oshell
oshell / Cards.jsx
Created March 19, 2020 17:31
Reactn Function Component
import React, { useGlobal } from 'reactn'; // <-- reactn
import Card from '../card/card';
const Cards = () => {
const [cards, setCards] = useGlobal('cards');
return (
<div>
{cards.map(card => (
<Card key={card.id} {...card} />
@oshell
oshell / Cards.jsx
Created March 19, 2020 17:28
Reactn Class Example
import React from 'reactn'; // <-- reactn
import Card from '../card/card';
export default class Cards extends React.PureComponent {
componentDidMount() {
// Hydrate the global state with the response from /api/cards.
this.setGlobal(
fetch('/api/cards')
.then(response => response.json())
.then(cards => ({ cards }))
@oshell
oshell / App.test.js
Last active November 17, 2019 22:31
enzyme test of stateful functional component with async state change
import React from 'react';
import { shallow } from 'enzyme';
import App from './App';
it('button click changes color of box', async () => {
const app = shallow(<App />);
expect(app.find('.box').length).toEqual(1);
// cache button element
const button = app.find('button').last();
const eventMock = {
@oshell
oshell / App.test.js
Created November 17, 2019 20:54
react test with shallow rendering and event mock
import React from 'react';
import { shallow } from 'enzyme';
import App from './App';
it('button click changes color of box', () => {
const app = shallow(<App />);
expect(app.find('.box').length).toEqual(1);
// cache button element
const button = app.find('button').last();
// pass mocked event object
@oshell
oshell / App.js
Created November 17, 2019 20:42
sample stateful function component with data attributes
import React, { useState } from 'react';
import './App.css';
function App() {
const [color, changeColor] = useState('blue');
function handleClick(e) {
changeColor(e.target.getAttribute('data-color'));
}