Is there a way to model using pure rescript? guess no, as there is no class construct equivalent
@@jsxConfig({ version: 4, mode: “automatic” })
Correct. There’s not a way to create classes in pure rescript.
Is there a way to bind rescript to js class definitions?
Sure. In the React component case, it’s pretty easy. Here’s an implementation of an ErrorBoundary that should work in the playground:
module ErrorBoundary = {
%%raw(`
class ErrorBoundaryImpl extends React.Component {
constructor(props) {
super(props);
this.state = { hasError: false };
}
static getDerivedStateFromError(error) {
// Update state so the next render will show the fallback UI.
return { hasError: true };
}
componentDidCatch(error, info) {
// Example "componentStack":
// in ComponentThatThrows (created by App)
// in ErrorBoundary (created by App)
// in div (created by App)
// in App
console.log(error, info.componentStack);
}
render() {
if (this.state.hasError) {
// You can render any custom fallback UI
return this.props.fallback;
}
return this.props.children;
}
}
`)
@react.component
external make: (
~fallback: React.element,
~children: React.element
) => React.element = "ErrorBoundaryImpl"
}
let badCall: () => 'a = %raw(`
() => { throw "Oh boy." }
`)
module ChildComponent = {
@react.component
let make = (~shouldError) => {
if shouldError {
badCall()
}
<div style={{color: "green"}}>
{React.string("All good here!")}
</div>
}
}
module App = {
@react.component
let make = () =>
<div>
<ErrorBoundary fallback={<div style={{color: "red"}}>{React.string("Never needed!")}</div>}>
<ChildComponent shouldError={false} />
</ErrorBoundary>
<ErrorBoundary fallback={<div style={{color: "red"}}>{React.string("An error was thrown here!")}</div>}>
<ChildComponent shouldError={true} />
</ErrorBoundary>
</div>
}