Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for example being codesandboxed #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions example/codesandbox/complex.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// @flow
import React from 'react';
import { render } from 'react-dom';
import { Provider, Subscribe, Container } from 'unstated';

type AppState = {
amount: number
};

class AppContainer extends Container<AppState> {
state = {
amount: 1
};

setAmount(amount: number) {
this.setState({ amount });
}
}

type CounterState = {
count: number
};

class CounterContainer extends Container<CounterState> {
state = {
count: 0
};

increment(amount: number) {
this.setState({ count: this.state.count + amount });
}

decrement(amount: number) {
this.setState({ count: this.state.count - amount });
}
}

function Counter() {
return (
<Subscribe to={[AppContainer, CounterContainer]}>
{(app, counter) => (
<div>
<span>Count: {counter.state.count}</span>
<button onClick={() => counter.decrement(app.state.amount)}>-</button>
<button onClick={() => counter.increment(app.state.amount)}>+</button>
</div>
)}
</Subscribe>
);
}

function App() {
return (
<Subscribe to={[AppContainer]}>
{app => (
<div>
<Counter />
<label>Amount: </label>
<input
type="number"
value={app.state.amount}
onChange={event => {
const amount = parseInt(event.currentTarget.value, 10);
if (Number.isNaN(amount)) {
return;
}
app.setAmount(amount);
}}
/>
</div>
)}
</Subscribe>
);
}

render(
<Provider>
<App />
</Provider>,
window.complex
);
14 changes: 14 additions & 0 deletions example/codesandbox/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Unstated - Examples</title>
</head>
<body>
<h3>Simple</h3>
<div id="simple"></div>

<h3>Complex</h3>
<div id="complex"></div>
</body>
</html>
2 changes: 2 additions & 0 deletions example/codesandbox/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import './simple.js';
import './complex.js';
18 changes: 18 additions & 0 deletions example/codesandbox/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "unstated-example",
"version": "0.0.0",
"private": true,
"dependencies": {
"create-react-context": "^0.1.4",
"react": "^16.2.0",
"react-dom": "^16.2.0",
"react-scripts": "1.1.0",
"unstated": "latest"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
}
}
2 changes: 1 addition & 1 deletion example/simple.js → example/codesandbox/simple.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// @flow
import React from 'react';
import { render } from 'react-dom';
import { Provider, Subscribe, Container } from '../src/unstated';
import { Provider, Subscribe, Container } from 'unstated';

type CounterState = {
count: number
Expand Down
18 changes: 18 additions & 0 deletions example/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "unstated-example",
"version": "0.0.0",
"private": true,
"main": "complex.js",
"dependencies": {
"react": "^16.2.0",
"react-dom": "^16.2.0",
"react-scripts": "1.1.0",
"unstated": "latest"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
}
}
81 changes: 81 additions & 0 deletions example/parcel/complex.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// @flow
import React from 'react';
import { render } from 'react-dom';
import { Provider, Subscribe, Container } from '../../src/unstated';

type AppState = {
amount: number
};

class AppContainer extends Container<AppState> {
state = {
amount: 1
};

setAmount(amount: number) {
this.setState({ amount });
}
}

type CounterState = {
count: number
};

class CounterContainer extends Container<CounterState> {
state = {
count: 0
};

increment(amount: number) {
this.setState({ count: this.state.count + amount });
}

decrement(amount: number) {
this.setState({ count: this.state.count - amount });
}
}

function Counter() {
return (
<Subscribe to={[AppContainer, CounterContainer]}>
{(app, counter) => (
<div>
<span>Count: {counter.state.count}</span>
<button onClick={() => counter.decrement(app.state.amount)}>-</button>
<button onClick={() => counter.increment(app.state.amount)}>+</button>
</div>
)}
</Subscribe>
);
}

function App() {
return (
<Subscribe to={[AppContainer]}>
{app => (
<div>
<Counter />
<label>Amount: </label>
<input
type="number"
value={app.state.amount}
onChange={event => {
const amount = parseInt(event.currentTarget.value, 10);
if (Number.isNaN(amount)) {
return;
}
app.setAmount(amount);
}}
/>
</div>
)}
</Subscribe>
);
}

render(
<Provider>
<App />
</Provider>,
window.complex
);
File renamed without changes.
41 changes: 41 additions & 0 deletions example/parcel/simple.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// @flow
import React from 'react';
import { render } from 'react-dom';
import { Provider, Subscribe, Container } from '../../src/unstated';

type CounterState = {
count: number
};

class CounterContainer extends Container<CounterState> {
state = { count: 0 };

increment() {
this.setState({ count: this.state.count + 1 });
}

decrement() {
this.setState({ count: this.state.count - 1 });
}
}

function Counter() {
return (
<Subscribe to={[CounterContainer]}>
{counter => (
<div>
<button onClick={() => counter.decrement()}>-</button>
<span>{counter.state.count}</span>
<button onClick={() => counter.increment()}>+</button>
</div>
)}
</Subscribe>
);
}

render(
<Provider>
<Counter />
</Provider>,
window.simple
);
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"format": "prettier --write **/*.{js,json,md}",
"prepublish": "yarn run build",
"precommit": "lint-staged",
"example": "parcel example/index.html"
"example": "parcel example/parcel/index.html"
},
"peerDependencies": {
"create-react-context": "^0.1.4",
Expand Down