forked from vercel/next.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
102 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
module type Config = { | ||
type context; | ||
let defaultValue: context; | ||
}; | ||
|
||
module Make = (Config: Config) => { | ||
let x = React.createContext(Config.defaultValue); | ||
|
||
module Provider = { | ||
let make = x->React.Context.provider; | ||
|
||
[@bs.obj] | ||
external makeProps: | ||
( | ||
~value: Config.context, | ||
~children: React.element, | ||
~key: string=?, | ||
unit | ||
) => | ||
{ | ||
. | ||
"value": Config.context, | ||
"children": React.element, | ||
} = | ||
""; | ||
}; | ||
}; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
type reducer = (CountContext.state, CountContext.action => unit); | ||
|
||
[@react.component] | ||
let make = () => { | ||
let ({count}, dispatch): reducer = React.useContext(CountContext.x); | ||
|
||
<div> | ||
<div> {j|This is the count: $count|j}->React.string </div> | ||
<button onClick={_e => dispatch(CountContext.Increment)}> | ||
"Increment count"->React.string | ||
</button> | ||
</div>; | ||
}; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
type state = {count: int}; | ||
type action = | ||
| Increment; | ||
let init = {count: 0}; | ||
let reducer = (state, action) => { | ||
switch (action) { | ||
| Increment => {count: state.count + 1} | ||
}; | ||
}; | ||
|
||
type t = (state, action => unit); | ||
|
||
include Context.Make({ | ||
type context = t; | ||
let defaultValue = ( | ||
init, | ||
_ => { | ||
(); | ||
}, | ||
); | ||
}); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[@react.component] | ||
let make = (~children) => { | ||
let reducer = React.useReducer(CountContext.reducer, CountContext.init); | ||
|
||
<CountContext.Provider value=reducer> children </CountContext.Provider>; | ||
}; |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import App, { Container } from 'next/app' | ||
import React from 'react' | ||
import { make as CountProvider } from '../components/CountProvider.bs' | ||
|
||
class MyApp extends App { | ||
static async getInitialProps({ Component, router, ctx }) { | ||
let pageProps = {} | ||
|
||
if (Component.getInitialProps) { | ||
pageProps = await Component.getInitialProps(ctx) | ||
} | ||
|
||
return { pageProps } | ||
} | ||
|
||
render() { | ||
const { Component, pageProps, store } = this.props | ||
return ( | ||
<Container> | ||
<CountProvider> | ||
<Component {...pageProps} /> | ||
</CountProvider> | ||
</Container> | ||
) | ||
} | ||
} | ||
|
||
export default MyApp |