File tree Expand file tree Collapse file tree 6 files changed +102
-7
lines changed Expand file tree Collapse file tree 6 files changed +102
-7
lines changed Original file line number Diff line number Diff line change 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+ };
Original file line number Diff line number Diff line change 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+ };
Original file line number Diff line number Diff line change 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+ });
Original file line number Diff line number Diff line change 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+ };
Original file line number Diff line number Diff line change 33 * Add updates the components internal state.
44 */
55type 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
1614let 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 ]
2220let 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
Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments