This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Store } from "pullstate"; | |
export const UIStore = new Store({ | |
isDarkMode: true, | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} /> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 })) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useState } from 'react'; | |
import './App.css'; | |
function App() { | |
const [color, changeColor] = useState('blue'); | |
function handleClick(e) { | |
changeColor(e.target.getAttribute('data-color')); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import { shallow } from 'enzyme'; | |
import App from './App'; | |
it('button changes color of box', () => { | |
const app = shallow(<App />); | |
expect(app.find('.box').length).toEqual(1); | |
app.find('button').last().simulate('click'); | |
expect(app.find('.box.red').length).toEqual(1); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useState } from 'react'; | |
import './App.css'; | |
function App() { | |
const [color, changeColor] = useState('blue'); | |
return ( | |
<div className="App"> | |
<button onClick={changeColor.bind(null, 'blue')}>blue</button> | |
<button onClick={changeColor.bind(null, 'green')}>green</button> |
NewerOlder