### ReasonReact Basic Greeting Component Source: https://reasonml.github.io/reason-react/docs/en/simple Demonstrates a simple ReasonReact component that renders a greeting message. It highlights Reason's implicit returns, labeled parameters, and type casting for strings. ```reason /* Greeting.re */ [@react.component] let make = (~message) =>

{React.string(message)}

; ``` ```reason /* ... */
/* ... */ ``` -------------------------------- ### Equivalent JavaScript Usage of ReasonReact Component Source: https://reasonml.github.io/reason-react/docs/en/intro-example Illustrates the equivalent way to render the 'Greeting' ReasonReact component in a plain JavaScript index file, demonstrating how ReasonReact components are used in a JavaScript context. ```javascript /* file: index.js */ let root = document.getElementById("root"); if (root != null) { ReactDOM.render(, root); }; ``` -------------------------------- ### Import ReasonReact Component into JavaScript/TypeScript App Source: https://reasonml.github.io/reason-react/docs/en/intro-example Shows how to import a ReasonReact component (transpiled to JavaScript) into an existing JavaScript or TypeScript application. It assumes the component exports a function named 'make'. ```javascript /* file: App.js */ import { make as Greeting } from "./Greeting.js"; export default function App() { return (
); } ``` -------------------------------- ### Define ReasonReact Component: Greeting Source: https://reasonml.github.io/reason-react/docs/en/intro-example Defines a simple ReasonReact functional component named 'Greeting' that accepts a 'name' prop and renders a button with a personalized greeting. Requires the 'react' binding. ```reason /* file: Greeting.re */ [@react.component] let make = (~name) => ; ``` -------------------------------- ### ReasonReact Component Rendering a List with Belt Source: https://reasonml.github.io/reason-react/docs/en/simple Demonstrates rendering a list of items in ReasonReact using the Belt library for array mapping. It defines a record type for list items and shows how to map and render them as list elements. ```reason /* We define the type of the item (this is a record) */ type item = { id: string, text: string, }; [@react.component] let make = (~items) => ; ``` -------------------------------- ### ReasonReact Component with Optional Arguments and Fragments Source: https://reasonml.github.io/reason-react/docs/en/simple Illustrates how to create a ReasonReact component with an optional argument and use React.Fragment. It shows conditional rendering based on the presence of the optional argument. ```reason [@react.component] let make = (~title, ~description=?) => <> /* React.Fragment works the same way as in React.js! */

title

/* Handling optional variables where you don't want to render anything */ { switch (description) { | Some(description) => {React.string(description)} /* Since everything is typed, React.null is required */ | None => React.null } } ; ``` -------------------------------- ### Use ReasonReact Component in Reason App Source: https://reasonml.github.io/reason-react/docs/en/intro-example Renders the 'Greeting' ReasonReact component within a Reason application's index file using ReactDOM.render. It selects the root element by its ID and handles the case where the element is not found. ```reason /* file: Index.re */ switch (ReactDOM.querySelector("#root")) { | Some(root) => ReactDOM.render(, root) | None => () } ``` -------------------------------- ### ReasonReact Simple Counter Example with useState Source: https://reasonml.github.io/reason-react/docs/en/usestate-hook A basic counter component implemented using the `useState` hook in ReasonReact. It demonstrates how to initialize state, display it, and update it using buttons for incrementing, decrementing, and resetting. ```reasonml [@react.component] let make = (~initialCount) => { let (count, setCount) = React.useState(_ => initialCount);
{React.string("Count: " ++ Int.to_string(count))}
; }; ``` -------------------------------- ### ReasonReact Form Component Source: https://reasonml.github.io/reason-react/docs/en/simple Shows a ReasonReact form component, demonstrating event handling for form submission and input element attributes. It highlights differences like `type_` for reserved words and automatic prop conversion. ```reason [@react.component] let make = () => { /* unused variables are prefixed with an underscore */ let onSubmit = _event => Js.log("Hello this is a log!"); /* onSubmit=onSubmit turns to just onSubmit */
; }; ``` -------------------------------- ### ReasonReact cloneElement Usage Example Source: https://reasonml.github.io/reason-react/docs/en/clone-element Demonstrates how to use the `ReasonReact.cloneElement` function, mirroring the ReactJS `cloneElement` functionality. It shows creating a new element with modified props and children. Note that the `props` argument is marked as unsafe. ```reasonml let clonedElement = React.cloneElement(
, ~props={"payload": 1}, [||]); ``` -------------------------------- ### Handle Optional Data with Belt.Option.mapWithDefault Source: https://reasonml.github.io/reason-react/docs/en/working-with-optional-data This example shows how to safely extract a value from an optional type using Belt.Option.mapWithDefault. If the optional value is present, it applies a function to it; otherwise, it returns a default value. ```reasonml let label = selected->Belt.Option.mapWithDefault("Select a Color", selected => selected##name); ``` -------------------------------- ### Apollo Client Integration with ReasonReact and graphql-ppx Source: https://reasonml.github.io/reason-react/docs/en/graphql-apollo This example shows how to use Apollo Client's useQuery hook within a ReasonReact component. It combines graphql-ppx for query definition with Apollo Hooks to fetch user data. The component handles different data states (Loading, Data, NoData, Error) using Reason's pattern matching for type-safe rendering. ```reason /* Username.re */ module UserQuery = [%graphql {| query UserQuery { currentUser { name } } |}]; [@react.component] let make = () => { let (currentUserName, _) = ApolloHooks.useQuery(UserQuery.definition);
{ switch(currentUserName) { | Loading =>

{React.string("Loading...")}

| Data(data) =>

{React.string(data##currentUser##name)}

| NoData | Error(_) =>

{React.string("Get off my lawn!")}

} }
} ``` -------------------------------- ### Invalid GraphQL Query Example with graphql-ppx Source: https://reasonml.github.io/reason-react/docs/en/graphql-apollo Illustrates an intentionally invalid GraphQL query to show how graphql-ppx catches errors at compile time. This example intentionally uses an incorrect field name ('namee') which would result in a compiler error, preventing runtime issues. ```reason /* Username.re */ module UserQuery = [%graphql {| query UserQuery { currentUser { namee // ERROR: Unknown field on type currentUser } } |}]; ``` -------------------------------- ### ReasonReact Children Handling Example Source: https://reasonml.github.io/reason-react/docs/en/jsx Illustrates how ReasonReact passes multiple children as an array and unwraps a single child for convenience when rendering components. ```reason
``` ```reason
``` -------------------------------- ### Reason React Component Naming Conventions Source: https://reasonml.github.io/reason-react/docs/en/components This example showcases how Reason React automatically names components based on their module and declaration. It illustrates naming for a top-level 'make' function, a 'component' function, and a 'make' function within a nested module. It also shows how to manually set a display name using React.setDisplayName. ```reason /* File.re */ /* will be named `File` in dev tools */ [@react.component] let make = ... /* will be named `File$component` in dev tools */ [@react.component] let component = ... module Nested = { /* will be named `File$Nested` in dev tools */ [@react.component] let make = ... }; /* Example of manual naming */ let make = ...; React.setDisplayName(make, "NameThatShouldBeInDevTools"); ``` -------------------------------- ### ReasonReact useEffect0: Cleanup Effect Source: https://reasonml.github.io/reason-react/docs/en/useeffect-hook Demonstrates how to use React.useEffect0 in ReasonReact to set up an effect that requires cleanup. The cleanup function is returned as an option, ensuring resources like subscriptions are properly released when the component unmounts or the effect re-runs. This is useful for one-time setup and teardown. ```reason [@react.component] let make = () => { React.useEffect0(() => { let id = subscription.subscribe(); /* clean up the subscription */ Some(() => subscription.unsubscribe(id)); }); } ``` -------------------------------- ### ReasonReact Usage of Spread Component Source: https://reasonml.github.io/reason-react/docs/en/adding-data-props An example demonstrating how to use the 'Spread' component in ReasonReact. It shows passing a 'data-cy' attribute via the 'props' argument to the Spread component, which then applies it to its child 'div' element. ```reasonml [@react.component] let make = () => /* This div will now have the `data-cy` attribute in the DOM! */
; ``` -------------------------------- ### Bind to External JavaScript Context in ReasonReact Source: https://reasonml.github.io/reason-react/docs/en/context This example shows how to bind to a React context defined in a separate JavaScript file within your ReasonReact application. It uses `[@bs.module]` to import the context and then consumes it using `React.useContext`, demonstrating interoperability between ReasonReact and JavaScript contexts. ```javascript /** ComponentThatDefinesTheContext.js */ export const ThemeContext = React.createContext("light"); ``` ```reason /** ComponentToConsumeTheContext.re */ [@bs.module "ComponentThatDefinesTheContext"] external themeContext: React.Context.t(string) = "ThemeContext"; [@react.component] let make = () => { let theme = React.useContext(themeContext);

theme->React.string

} ``` -------------------------------- ### ReasonReact Component with Tailwind CSS Styling Source: https://reasonml.github.io/reason-react/docs/en/tailwind-css This example showcases a ReasonReact component utilizing Tailwind CSS classes for styling. It demonstrates how to apply common Tailwind utilities for layout, appearance, and text formatting within a React component structure. ```reason [@react.component] let make = () =>
Sunset in the mountains
{React.string("Tailwind")}

{React.string("A reason react starter with tailwind")}

; ``` -------------------------------- ### ReasonReact useEffect1: Conditional Effect Firing Source: https://reasonml.github.io/reason-react/docs/en/useeffect-hook Illustrates using React.useEffect1 in ReasonReact for effects that depend on specific values. The effect will only re-run if the dependencies listed in the array change. This is crucial for performance, preventing unnecessary re-executions of effects. The example shows setting up and cleaning up a subscription based on a 'source' prop. ```reason [@react.component] let make = (~source) => { React.useEffect1(() => { let id = subscription.subscribe(); /* clean up the subscription */ Some(() => subscription.unsubscribe(id)); }, [|source|]); } ``` -------------------------------- ### Route Matching with Pattern Matching in ReasonReact Source: https://reasonml.github.io/reason-react/docs/en/router Demonstrates how to match different URL paths using Reason's powerful pattern matching capabilities. This allows for flexible handling of various routes and sub-routes within a ReasonReact application. ```reason let url = ReasonReactRouter.useUrl(); switch (url.path) { | ["book", id, "edit"] => handleBookEdit(id) | ["book", id] => getBook(id) | ["book", id, _] => noSuchBookOperation() | [] => showMainPage() | ["shop"] | ["shop", "index"] => showShoppingPage() | ["shop", ...rest] => /* e.g. "shop/cart/10", but let "cart/10" be handled by another function */ nestedMatch(rest) | _ => showNotFoundPage() }; ``` -------------------------------- ### Passing Components as Props in ReasonReact Source: https://reasonml.github.io/reason-react/docs/en/component-as-prop Demonstrates how to pass components as props in ReasonReact by using a callback function. This approach allows the owner to control how the component renders and avoids the need for prop spreading within the child component. ```reasonml let bannerCallback = (prop1, prop2) => ; ; ``` -------------------------------- ### Render and Test ReasonReact Components with ReactDOMTestUtils Source: https://reasonml.github.io/reason-react/docs/en/testing This snippet demonstrates how to write a basic test for ReasonReact components using ReactDOMTestUtils and an imaginary TestFramework. It covers setting up a container, rendering a component with `act`, and asserting its presence in the DOM using provided querying utilities. ```reasonml open ReactDOMTestUtils; open TestFramework; // TestFramework isn't a real module, just an imaginary set of bindings // to a JavaScript testing framework describe("My basic test", ({test, beforeEach, afterEach}) => { // Here, we prepare an empty ref that will eventually be // the root node for our test let container = ref(None); // Before each test, creates a new div root beforeEach(prepareContainer(container)); // After each test, removes the div afterEach(cleanupContainer(container)); test("can render DOM elements", ({expect}) => { // The following function gives us the div let container = getContainer(container); let root = ReactDOM.Client.createRoot(container); // Most of the ReactDOMTestUtils API is there act(() => { ReactDOM.Client.render(root,
"Hello world!"->React.string
); }); expect.bool( container // We also provide some basic DOM querying utilities // to ease your tests ->DOM.findBySelectorAndTextContent("div", "Hello world!") ->Option.isSome, ). toBeTrue(); }); }); ``` -------------------------------- ### Using ContextProvider in ReasonReact Application Source: https://reasonml.github.io/reason-react/docs/en/context This code shows how to use the `ContextProvider` component to wrap a part of your ReasonReact application. Any component rendered within `ContextProvider` will have access to the provided context value. This is a common pattern for managing global state like themes or user authentication. ```reason /** App.re */ [@react.component] let make = () =>
``` -------------------------------- ### Create React Component with ReasonReact Source: https://reasonml.github.io/reason-react/docs/en/reason-using-js This snippet demonstrates how to create a React component using ReasonReact. It shows the JavaScript implementation and its corresponding ReasonML definition, enabling interoperability between the two languages. ```javascript var ReactDOM = require('react-dom'); var React = require('react'); var MyBanner = function(props) { if (props.show) { return React.createElement('div', null, 'Here\'s the message from the owner: ' + props.message ); } else { return null; } }; module.exports = MyBanner; ``` ```reasonml /* ReactJS used by ReasonReact */ [@react.component] [@bs.module] external make : (~show: bool, ~message: string) => React.element = "./MyBanner"; ``` -------------------------------- ### Basic Render Props with ReasonReact Source: https://reasonml.github.io/reason-react/docs/en/render-props Demonstrates a basic implementation of render props in ReasonReact. The Loader component accepts a `render` function prop to define its content. ```reasonml [@react.component] let make = () => {
} /> }; ``` ```reasonml /* Loader.re */ [@react.component] let make = (~render) => {
{render()}
}; ``` -------------------------------- ### ReasonReact Ternary Shortcut for Conditional Rendering Source: https://reasonml.github.io/reason-react/docs/en/ternary-shortcut Demonstrates the recommended way to perform conditional rendering in ReasonReact using the ternary operator. This pattern ensures proper handling of null rendering, unlike the shortcut often used in JavaScript. It contrasts with the ReactJS `&&` pattern. ```reason showButton ?
}; ``` -------------------------------- ### Import Javascript file into ReasonReact Source: https://reasonml.github.io/reason-react/docs/en/importing-js-into-reason Demonstrates how to import a JavaScript file as a React component into ReasonReact. It requires the use of the [@mel.module] attribute to specify the path to the JavaScript file and [@react.component] to denote it as a React component. ```javascript /* MyJavascriptFile.js */ export default function Greeting({ name }) { return Hey {name} } ``` ```reason /* App.re */ module Greeting = { [@mel.module "./MyJavascriptFile.js"] [@react.component] external make: (~name: string) => React.element = "default"; }; let make = () =>
; ``` -------------------------------- ### Create DOM Element with Variable Tag in ReasonReact Source: https://reasonml.github.io/reason-react/docs/en/i-want-to-create-a-dom-element-without-jsx Illustrates how to dynamically create DOM elements in ReasonReact by first assigning a string tag, converted to a component using ReactDOM.stringToComponent, to a variable. This variable can then be passed to React.createElement. ```reasonml let tag = ReactDOM.stringToComponent(multiline ? "textarea" : "input"); React.createElement(tag, /* ... */) ``` -------------------------------- ### Export Reason React Component to JavaScript Source: https://reasonml.github.io/reason-react/docs/en/components This snippet shows how to use a Reason React component (exported as 'make') within JavaScript code. It assumes the component is available via a CommonJS require. ```javascript const MyComponent = require('./path/to/Component.js').make; ``` -------------------------------- ### Handle Invalid Prop Names in ReasonReact Source: https://reasonml.github.io/reason-react/docs/en/invalid-prop-name Reason/OCaml keywords like `type`, `as`, `open`, `begin`, `end`, `in`, and `to` are invalid as prop names. ReasonReact requires appending an underscore (e.g., `type_`, `as_`) for DOM elements. For `aria-*` and `data-*` attributes, specific casing and handling are recommended. ```reason let example = ; let anotherExample =
; ``` -------------------------------- ### ReasonReact @react.component Decorator Explanation Source: https://reasonml.github.io/reason-react/docs/en/components Illustrates how the `[@react.component]` decorator transforms a ReasonReact component definition into a JavaScript-compatible function. It generates a `makeProps` function for handling named arguments as a JavaScript object, which is how React expects props. ```reasonml /* This is the generated code structure */ [@mel.obj] external makeProps: (~name: 'name, ~key: string=?, unit) => {. "name": 'name} = ""; let make = (Props) => { let name = Props##name; let (count, setCount) = React.useState(() => 0);

{React.string(name ++ " clicked " ++ Belt.Int.toString(count) ++ " times")}

}; ``` -------------------------------- ### ReasonReact ReactDOM: Hydration Source: https://reasonml.github.io/reason-react/docs/en/dom Performs hydration, converting a static HTML page into a dynamic React app using ReasonReact's ReactDOM module. It selects a root DOM element and uses `ReactDOM.hydrateRoot` to attach React event listeners to the existing HTML. ```reason let element = ReactDOM.querySelector("#root"); switch (element) { | None => Js.log("#root element not found"); | Some(element) => { /* root is a ReactDOM.Client.root and used to render on top of the existing HTML with ReactDOM.Client.render or unmount, via ReactDOM.Client.unmount. */ let _root = ReactDOM.hydrateRoot(, element); (); } } ``` -------------------------------- ### ReasonReact JSX to JavaScript Transformation (Prop-less Capitalized) Source: https://reasonml.github.io/reason-react/docs/en/jsx Details the transformation of a prop-less capitalized JSX element, showing how it compiles to a JavaScript call using React.jsx with the component's `make` function. ```reason ``` ```javascript React.jsx( MyReasonComponent.make, MyReasonComponent.makeProps(), ) ``` ```javascript React.jsx(MyReasonComponent.make, {}) ``` -------------------------------- ### Import JavaScript Component into Reason React Source: https://reasonml.github.io/reason-react/docs/en/components This demonstrates how to import a default-exported JavaScript component into Reason React using an 'external' declaration. The [@mel.module] attribute specifies the file path, and [@react.component] helps generate the necessary props and component functions. ```reason [@mel.module "./path/to/Component.js"] [@react.component] external make: (~name: string) => React.element = "default"; ``` -------------------------------- ### ReasonReact useState Hook Signature Source: https://reasonml.github.io/reason-react/docs/en/usestate-hook The signature for the `useState` hook in ReasonReact. It takes a function that returns the initial state and returns a tuple containing the current state and a function to update it. This differs from React.js by requiring a function for the initial state. ```reasonml let useState: (unit => 'state) => ('state, 'state => unit); ``` -------------------------------- ### ReasonReact JSX to JavaScript Transformation (Capitalized) Source: https://reasonml.github.io/reason-react/docs/en/jsx Illustrates the transformation of capitalized JSX elements (custom components) into ReasonReact's JavaScript output. It handles props, children, and the use of `make` and `makeProps` functions. ```reason {child1} {child2} ``` ```javascript React.jsxs( MyReasonComponent.make, MyReasonComponent.makeProps( ~ref=b, ~foo=bar, ~baz=qux, ~children=[|child1, child2|], () ), ) ``` ```javascript React.jsxs( MyReasonComponent.make, { ref: b, foo: bar, baz: qux, children: [ child1, child2 ], }, ) ``` -------------------------------- ### ReasonReact useReducer Hook Implementation Source: https://reasonml.github.io/reason-react/docs/en/usereducer-hook This snippet demonstrates the implementation of the `useReducer` hook in ReasonReact. It defines an action type with two cases (Increment and Decrement) and a reducer function to manage state updates. The `make` component utilizes `React.useReducer` to handle state and dispatch actions, rendering the current state and buttons to trigger state changes. ```reasonml /* we can create anything as the type for action, here we use a variant with 2 cases. */ type action = | Increment | Decrement; /* `state` could also be anything. In this case, we want an int */ let reducer = (state, action) => switch (action) { | Increment => state + 1 | Decrement => state - 1 }; [@react.component] let make = (~initialValue=0) => { let (state, dispatch) = React.useReducer(reducer, initialValue);
state->React.int
; }; ``` -------------------------------- ### ReasonReact JSX to JavaScript Transformation (Prop-less Uncapitalized) Source: https://reasonml.github.io/reason-react/docs/en/jsx Shows the transformation of a prop-less uncapitalized JSX element (`
`) into JavaScript, using ReactDOM.jsx and compiling to React.createElement. ```reason
``` ```javascript ReactDOM.jsx("div", ReactDOM.domProps()); ``` ```javascript React.createElement('div', {}) ``` -------------------------------- ### ReasonReact Rendering Primitives Source: https://reasonml.github.io/reason-react/docs/en/components Shows how to explicitly render different data types like strings, integers, floats, arrays, and null within a ReasonReact component's JSX. Specific functions like `React.string`, `React.int`, `React.float`, and `React.array` are used for type safety. ```reasonml
{React.string("Hello")} {React.int(3)} {React.float(1.23)} {React.array([|
, |])} {React.null}
``` -------------------------------- ### Create DOM Element with React.createElement (JavaScript) Source: https://reasonml.github.io/reason-react/docs/en/i-want-to-create-a-dom-element-without-jsx Demonstrates the JavaScript approach to creating DOM elements using React.createElement, where a string can represent a DOM tag. This is useful when the tag needs to be determined dynamically. ```javascript React.createElement(multiline ? "textarea" : "input" /* ... */); ``` -------------------------------- ### ReasonReact JSX to JavaScript Transformation (Uncapitalized) Source: https://reasonml.github.io/reason-react/docs/en/jsx Demonstrates how ReasonReact's ppx transforms uncapitalized JSX elements into JavaScript calls using ReactDOM.jsxs and ReactDOM.domProps, and its compiled output. It handles nested children and prop transformations. ```reason
{child1} {child2}
``` ```javascript ReactDOM.jsxs( "div", ReactDOM.domProps( ~children=React.array([|child1, child2|]), ~foo=bar, (), ) ) ``` ```javascript React.jsx('div', {foo: bar, children: [ child1, child2 ] }) ``` -------------------------------- ### React.js Input Change Handler Source: https://reasonml.github.io/reason-react/docs/en/usestate-hook A standard React.js component demonstrating how to handle input changes using the `useState` hook. It directly accesses the event target value within the `onChange` handler. ```javascript /* App.js */ function App() { const [name, setName] = React.useState("John"); return ( setName(event.target.value)} /> ); } ``` -------------------------------- ### Correct ReasonReact Input Change Handler with useState Source: https://reasonml.github.io/reason-react/docs/en/usestate-hook The corrected ReasonReact implementation for handling input changes. It resolves the bug by extracting the event's value into a local variable *before* calling the state setter function, ensuring the value is available when needed. ```reasonml /* App.re */ /* CORRECT! This code is bug-free. 👍 */ [@react.component] let make = () => { let (name, setName) = React.useState(() => "John"); { let value = React.Event.Form.target(event)##value; setName(_ => value) } } />; }; ```