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

Examples/using unstated #6920

Closed
wants to merge 9 commits into from
Closed
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
16 changes: 16 additions & 0 deletions examples/using-unstated/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Unstated

https://using-unstated.gatsbyjs.org/

Gatsby example site that shows use of unstated.

# Overview

To use unstated in a Gatsby site you'll need to hook in to two of Gatsby's
extension points.

Once in `replaceRenderer` which runs during Gatsby's server rendering process,
and once in `replaceRouterComponent` which is part of Gatsby's browser APIs.

Check out [`./gatsby-ssr.js`](./gatsby-ssr.js) and
[`./gatsby-browser.js`](./gatsby-browser.js) to see how this is implemented in this example.
14 changes: 14 additions & 0 deletions examples/using-unstated/gatsby-browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import React from 'react'
import { Router } from 'react-router-dom'
import { Provider } from 'unstated'

export const replaceRouterComponent = ({ history }) => {

const ConnectedRouterWrapper = ({ children }) => (
<Provider>
<Router history={history}>{children}</Router>
</Provider>
)

return ConnectedRouterWrapper
}
5 changes: 5 additions & 0 deletions examples/using-unstated/gatsby-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
siteMetadata: {
title: `Gatsby Unstated`,
},
}
13 changes: 13 additions & 0 deletions examples/using-unstated/gatsby-ssr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import React from 'react'
import { Provider } from 'unstated'
import { renderToString } from 'react-dom/server'

export const replaceRenderer = ({ bodyComponent, replaceBodyHTMLString }) => {

const ConnectedBody = () => (
<Provider>
{bodyComponent}
</Provider>
)
replaceBodyHTMLString(renderToString(<ConnectedBody/>))
}
23 changes: 23 additions & 0 deletions examples/using-unstated/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "gatsby-example-unstated",
"private": true,
"description": "Gatsby example site that shows use of unstated.",
"version": "1.0.0",
"author": "greg lobinski <greglobinski@gmail.com>",
"dependencies": {
"gatsby": "next",
"react": "^16.4.0",
"react-dom": "^16.4.0",
"unstated": "^2.1.1"
},
"keywords": [
"gatsby"
],
"license": "MIT",
"main": "n/a",
"scripts": {
"develop": "gatsby develop",
"build": "gatsby build",
"serve": "gatsby serve"
}
}
39 changes: 39 additions & 0 deletions examples/using-unstated/src/components/layout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import React from "react"
import { Link } from "gatsby"
import { Subscribe } from "unstated"
import CounterContainer from "../state/CounterContainer"

class DefaultLayout extends React.Component {
render() {
return (
<div>
<Link to="/">
<h3>Unstated example</h3>
</Link>
<Subscribe to={[CounterContainer]}>
{counter => (
<div>
<button onClick={() => counter.decrement()}>-</button>
<span>Count: {counter.state.count}</span>
<button onClick={() => counter.increment()}>+</button>
</div>
)}
</Subscribe>
<ul>
<li>
<Link to="/a/">a</Link>
</li>
<li>
<Link to="/b/">b</Link>
</li>
<li>
<Link to="/c/">c</Link>
</li>
</ul>
{this.props.children}
</div>
)
}
}

export default DefaultLayout
10 changes: 10 additions & 0 deletions examples/using-unstated/src/pages/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import React from "react"
import Layout from "../components/layout"

const A = () => (
<Layout>
<p>Page A</p>
</Layout>
)

export default A
10 changes: 10 additions & 0 deletions examples/using-unstated/src/pages/b.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import React from "react"
import Layout from "../components/layout"

const B = () => (
<Layout>
<p>Page B</p>
</Layout>
)

export default B
10 changes: 10 additions & 0 deletions examples/using-unstated/src/pages/c.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import React from "react"
import Layout from "../components/layout"

const C = () => (
<Layout>
<p>Page C</p>
</Layout>
)

export default C
10 changes: 10 additions & 0 deletions examples/using-unstated/src/pages/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import React from "react"
import Layout from "../components/layout"

const Home = () => (
<Layout>
<p>Home</p>
</Layout>
)

export default Home
17 changes: 17 additions & 0 deletions examples/using-unstated/src/state/CounterContainer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Container } from "unstated"

class CounterContainer extends Container {
state = {
count: 0,
}

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

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

export default CounterContainer