Hi @ all,
I am currently struggling a bit with the context api of reason react.
Here is my implementation of a context provider, which provides a function to all children:
let context =
React.createContext(
(
~typ: Notification.errorType,
~title: string,
~message: string,
~timeout: option(int),
) =>
()
);
let makeProps =
(
~value:
(
~typ: Notification.errorType,
~title: string,
~message: string,
~timeout: option(int)
) =>
unit,
~children,
(),
) => {
"children": children,
"value": value,
};
let make = React.Context.provider(context);
It is working completely fine, but I find it a bit wierd, that I have to type the function two times.
I mean the final implementation of the function also is in another file (because it is setting state in another component)…
<NotificationContext
value={(
~typ: Notification.errorType,
~title: string,
~message: string,
~timeout: option(int),
) => {
setMessages(currentMessages => {
Array.append(
currentMessages,
[|
{
"id": Js.Date.make()->Js.Date.toUTCString,
"typ": typ,
"title": title,
"message": message,
"timeout": timeout,
},
|],
)
})
}}>
...
…and here I am typing the parameters again
So if I want to, make the message
optional, I would have to change the type three times.
I am just asking myself if there needs to be an initial value for the React.createContext()
, if it is also not allowed, to leave out the value of the (in my case) <NotificationContext>
call.
But when leaving the React.createContext()
empty, it expects to recieve a unit
to be passed, which then leads to the question, why I need to type it in makeProps
when the value is obviously inferred.
Sorry…I am just a bit confused and wanted to share my struggles.
Is there something I am missing or is this just this cumbersome at the moment?
Thank you,
Torben