Skip to content

Commit

Permalink
Provide example using context API
Browse files Browse the repository at this point in the history
  • Loading branch information
arecvlohe committed May 15, 2019
1 parent c250e97 commit 03e7282
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 7 deletions.
27 changes: 27 additions & 0 deletions examples/with-reasonml/components/Context.re
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,
} =
"";
};
};
13 changes: 13 additions & 0 deletions examples/with-reasonml/components/CountConsumer.re
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>;
};
21 changes: 21 additions & 0 deletions examples/with-reasonml/components/CountContext.re
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,
_ => {
();
},
);
});
6 changes: 6 additions & 0 deletions examples/with-reasonml/components/CountProvider.re
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>;
};
14 changes: 7 additions & 7 deletions examples/with-reasonml/components/Counter.re
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,22 @@
* Add updates the components internal state.
*/
type action =
| Add
| Add;

/*
This is the components internal state representation. This state object
is unique to each instance of the component.
*/
type state = {
count: int,
};
type state = {count: int};

let counterReducer = (state, action) =>
switch(action) {
| Add => { count: state.count + 1 }
switch (action) {
| Add => {count: state.count + 1}
};

[@react.component]
let make = () => {
let (state, dispatch) = React.useReducer(counterReducer, { count: 0 });
let (state, dispatch) = React.useReducer(counterReducer, {count: 0});
let (globalState, globalDispatch) = GlobalCount.useGlobalCount();

let countMsg = "Count: " ++ string_of_int(state.count);
Expand All @@ -33,6 +31,8 @@ let make = () => {
<button onClick={_ => globalDispatch(GlobalCount.Increment)}>
{React.string("Add")}
</button>
<CountConsumer />
<CountConsumer />
</div>;
};

Expand Down
28 changes: 28 additions & 0 deletions examples/with-reasonml/pages/_app.js
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

0 comments on commit 03e7282

Please sign in to comment.