Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 0 additions & 113 deletions docs/callback-handlers.md

This file was deleted.

135 changes: 0 additions & 135 deletions docs/children.md

This file was deleted.

10 changes: 3 additions & 7 deletions docs/clone-element.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,13 @@
title: cloneElement
---

<aside class="warning">
The Record API is in feature-freeze. For the newest features and better support going forward, please consider migrating to the new <a href="https://reasonml.github.io/reason-react/docs/en/components">function components</a>.
</aside>
Signature: `let cloneElement: (React.element, ~props: Js.t({..})=?, 'anyChildrenType) => React.element`

Signature: `let cloneElement: (reactElement, ~props: Js.t({..})=?, 'anyChildrenType) => reactElement`

Same as ReactJS' [cloneElement](https://reactjs.org/docs/react-api.html#cloneelement). However, adding extra props to a ReasonReact component doesn't make sense; you'd use a [**render prop**](https://reactjs.org/docs/render-props.html). Therefore, `ReasonReact.cloneElement` is only used in edge-cases to convert over existing code.
Same as ReactJS' [cloneElement](https://reactjs.org/docs/react-api.html#cloneelement). However, adding extra props to a ReasonReact component doesn't make sense; you'd use a [**render prop**](https://reactjs.org/docs/render-props.html). Therefore, `ReasonReact.cloneElement` is only used in edge-cases.

```reason
let clonedElement =
ReasonReact.cloneElement(
React.cloneElement(
<div className="foo" />,
~props={"payload": 1},
[||]
Expand Down
27 changes: 14 additions & 13 deletions docs/components.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: Components
---

ReasonReact uses functions and [React Hooks](https://reactjs.org/docs/hooks-intro.html) to compose the component of your application. Let's look at how a component is written and then break down some of the things happening.
ReasonReact uses functions and [React Hooks](https://reactjs.org/docs/hooks-intro.html) to compose your components of your application. Let's look at how a component is written and then break down some of the things happening.

```reason
[@react.component]
Expand All @@ -20,10 +20,10 @@ let make = (~name) => {

## [@react.component]

This snippet is doing quite a bit! The first thing you might notice is the decorator attribute above the definition. `[@react.component]` tells ReasonReact that you're writing a component with named args syntax (`~name`), but that you would like to compile it into a function that takes a JS object as props which is how React works. Concretely, this attribute will generate code for you that looks like this:
This snippet is doing quite a bit! The first thing you might notice is the decorator attribute above the definition. `[@react.component]` tells ReasonReact that you're writing a component with named args syntax (`~name`), but that you would like to compile it into a function that takes a JavaScript object as props which is how React works. Concretely, this attribute will generate code for you that looks like this:

```reason
[@bs.obj]
[@mel.obj]
external makeProps: (~name: 'name, ~key: string=?, unit) => {. "name": 'name} = "";

let make = (Props) => {
Expand All @@ -39,7 +39,7 @@ let make = (Props) => {
};
```

It has added a new function called `makeProps` which uses [`[@bs.obj]`](https://melange.re/v2.0.0/communicate-with-javascript/#using-jst-objects) to create your props object. This function gets compiled away by Melange and will be replaced by object literals when used.
It has added a new function called `makeProps` which uses [mel-obj] to create your props object. This function gets compiled away by Melange and will be replaced by object literals when used.

### A note on `children`

Expand Down Expand Up @@ -113,7 +113,7 @@ Reason also always opts for the safest form of a given hook as well. So `React.u

## Hand-writing components

You don't need to use the `[@react.component]` declaration to write components. Instead you can write a pair of `foo` and `fooProps` functions such that `type fooProps: 'a => props and foo: props => React.element` and these will always work as React components! This works with your own version of [`[@bs.obj]`](https://melange.re/v2.0.0/communicate-with-javascript/#using-jst-objects), [`[bs.deriving abstract]`](https://melange.re/v2.0.0/communicate-with-javascript/#convert-records-into-abstract-types), or any other function that takes named args and returns a single props structure.
You don't need to use the `[@react.component]` declaration to write components. Instead you can write a pair of `foo` and `fooProps` functions such that `type fooProps: 'a => props and foo: props => React.element` and these will always work as React components! This works with your own version of [`[@mel.obj]`][mel-obj], or any other function that takes labelled arguments and returns a single prop object.

## Interop

Expand All @@ -122,33 +122,31 @@ You don't need to use the `[@react.component]` declaration to write components.
The make function above is a normal React component, you can use it today with code like:

```js
const MyComponent = require('./path/to/Component.bs.js').make;
const MyComponent = require('./path/to/Component.js').make;

<MyComponent name="Regina" />
```

### Import from JS

It also works seamlessly with [`[@genType]`](https://github.com/cristianoc/genType) annotations and can be integrated with safety into TypeScript and Flow applications.

Using a component written in JS requires a single external to annotate the types it takes.
Using a component written in JavaScript requires a single `external` to annotate the types it takes.

```reason
[@bs.module "./path/to/Component.js"][@react.component]
[@mel.module "./path/to/Component.js"] [@react.component]
external make: (~name: string) => React.element = "default";
```

This `[@react.component]` annotation will, again, generate both `make` and `makeProps` functions for you with the correct types. Here's an example of what this desugars to without `[@react.component]`:

```reason
[@bs.obj]
[@mel.obj]
external makeProps: (~name: 'name, ~key: string=?, unit) => {. "name": 'name} = "";

[@bs.module "./path/to/Component.js"]
[@mel.module "./path/to/Component.js"]
external make: ({. "name": string}) => React.element = "default";
```

**Note on `default`:** to understand what `default` means, see [the Melange docs on ES6](https://melange.re/v2.0.0/communicate-with-javascript/#default-es6-values).
**Note on `default`:** to understand what `default` means, see [the Melange docs on ES6][default-es6-values].

## Component Naming

Expand All @@ -173,3 +171,6 @@ module Nested = {
```

If you need a dynamic name for higher-order components or you would like to set your own name you can use `React.setDisplayName(make, "NameThatShouldBeInDevTools");`.

[mel-obj]: https://melange.re/v4.0.0/communicate-with-javascript#using-js-t-objects
[default-es6-values]: https://melange.re/v4.0.0/communicate-with-javascript#default-es6-values
Loading