I would like to define default values for an object passed to the React.createContext function:
// TreeContext.res
let context = React.createContext({
"selectedId": "", // I would like this to be optional
"theme": "dark"
})
let makeProps = (~value, ~children, ()) => {
"value": value,
"children": children,
};
let make = React.Context.provider(context);
Of course, the following code:
// Tree.res
@react.component
let make = () =>{
<TreeContext value={ "theme": "light" }>
{React.string("Hi there!")}
</TreeContext>
}
throws an error:
This has type: {"theme": string}
Somewhere wanted: {"selectedId": string}
The second object type has no method theme
// TreeContext.res
let context = React.createContext({
"attr1": Some("Value 1"), // I want this attr to be optional
"attr2": "Value 2"
})
module Provider = {
let provider = React.Context.provider(context)
@react.component
let make = (~value,~children) => {
React.createElement(provider, {"value": value, "children": children})
}
}
And I’d like to omit the attr1 attribute:
// Tree.res
@react.component
let make = () =>{
// I would like to omit "attr1", but Rescript forces me to write None
<TreeContext.Provider value={ "attr1": None, "attr2": "Some value" }>
{React.string("Hi there!")}
</TreeContext.Provider>
}
You’ll probably need to create a @obj based external for creating your JS object with a variable set of attributes (in this case, omit the attr1 value).
type context
@obj
external contextValue: (~attr1: string=?, ~attr2: string, unit) => context = ""
// Will create your `value` without an `attr1`
let context = React.createContext(contextValue(~attr2="Value2", ()))