Skip to content

Commit 03e7282

Browse files
committed
Provide example using context API
1 parent c250e97 commit 03e7282

File tree

6 files changed

+102
-7
lines changed

6 files changed

+102
-7
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
module type Config = {
2+
type context;
3+
let defaultValue: context;
4+
};
5+
6+
module Make = (Config: Config) => {
7+
let x = React.createContext(Config.defaultValue);
8+
9+
module Provider = {
10+
let make = x->React.Context.provider;
11+
12+
[@bs.obj]
13+
external makeProps:
14+
(
15+
~value: Config.context,
16+
~children: React.element,
17+
~key: string=?,
18+
unit
19+
) =>
20+
{
21+
.
22+
"value": Config.context,
23+
"children": React.element,
24+
} =
25+
"";
26+
};
27+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
type reducer = (CountContext.state, CountContext.action => unit);
2+
3+
[@react.component]
4+
let make = () => {
5+
let ({count}, dispatch): reducer = React.useContext(CountContext.x);
6+
7+
<div>
8+
<div> {j|This is the count: $count|j}->React.string </div>
9+
<button onClick={_e => dispatch(CountContext.Increment)}>
10+
"Increment count"->React.string
11+
</button>
12+
</div>;
13+
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
type state = {count: int};
2+
type action =
3+
| Increment;
4+
let init = {count: 0};
5+
let reducer = (state, action) => {
6+
switch (action) {
7+
| Increment => {count: state.count + 1}
8+
};
9+
};
10+
11+
type t = (state, action => unit);
12+
13+
include Context.Make({
14+
type context = t;
15+
let defaultValue = (
16+
init,
17+
_ => {
18+
();
19+
},
20+
);
21+
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[@react.component]
2+
let make = (~children) => {
3+
let reducer = React.useReducer(CountContext.reducer, CountContext.init);
4+
5+
<CountContext.Provider value=reducer> children </CountContext.Provider>;
6+
};

examples/with-reasonml/components/Counter.re

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,22 @@
33
* Add updates the components internal state.
44
*/
55
type action =
6-
| Add
6+
| Add;
77

88
/*
99
This is the components internal state representation. This state object
1010
is unique to each instance of the component.
1111
*/
12-
type state = {
13-
count: int,
14-
};
12+
type state = {count: int};
1513

1614
let counterReducer = (state, action) =>
17-
switch(action) {
18-
| Add => { count: state.count + 1 }
15+
switch (action) {
16+
| Add => {count: state.count + 1}
1917
};
2018

2119
[@react.component]
2220
let make = () => {
23-
let (state, dispatch) = React.useReducer(counterReducer, { count: 0 });
21+
let (state, dispatch) = React.useReducer(counterReducer, {count: 0});
2422
let (globalState, globalDispatch) = GlobalCount.useGlobalCount();
2523

2624
let countMsg = "Count: " ++ string_of_int(state.count);
@@ -33,6 +31,8 @@ let make = () => {
3331
<button onClick={_ => globalDispatch(GlobalCount.Increment)}>
3432
{React.string("Add")}
3533
</button>
34+
<CountConsumer />
35+
<CountConsumer />
3636
</div>;
3737
};
3838

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import App, { Container } from 'next/app'
2+
import React from 'react'
3+
import { make as CountProvider } from '../components/CountProvider.bs'
4+
5+
class MyApp extends App {
6+
static async getInitialProps({ Component, router, ctx }) {
7+
let pageProps = {}
8+
9+
if (Component.getInitialProps) {
10+
pageProps = await Component.getInitialProps(ctx)
11+
}
12+
13+
return { pageProps }
14+
}
15+
16+
render() {
17+
const { Component, pageProps, store } = this.props
18+
return (
19+
<Container>
20+
<CountProvider>
21+
<Component {...pageProps} />
22+
</CountProvider>
23+
</Container>
24+
)
25+
}
26+
}
27+
28+
export default MyApp

0 commit comments

Comments
 (0)