Skip to content

Commit aee491c

Browse files
committed
Add first React doc files
1 parent e64dbf3 commit aee491c

File tree

7 files changed

+680
-2
lines changed

7 files changed

+680
-2
lines changed
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
---
2+
title: Components and Props
3+
description: "Basic concepts for components and props in ReasonReact"
4+
canonical: "/docs/react/latest/components-and-props"
5+
category: "Main Concepts"
6+
---
7+
8+
# Components and Props
9+
10+
<Intro>
11+
12+
Components let you split the UI into independent, reusable pieces, and think about each piece in isolation. This page provides an introduction to the idea of components.
13+
14+
</Intro>
15+
16+
## What is a Component?
17+
18+
A React component is a function describing a UI element that receives a `props` object as a parameter (data describing the dynamic parts of the UI) and returns a `React.element`.
19+
20+
The nice thing about this concept is that you can solely focus on the input and output. The component function receives some data and returns some opaque `React.element` that is managed by the React framework to render your UI.
21+
22+
> If you want to know more about the low level details on how a component interface is implemented, refer to the advanced topic [React JSX Transformation](./react-jsx-transformation)
23+
24+
## Component Example
25+
26+
Let's start with a first example to see how a ReScript React component looks like:
27+
28+
<CodeTab labels={["ReScript", "JS Output"]}>
29+
30+
```res
31+
// src/Greeting.res
32+
@react.component
33+
let make = () => {
34+
<div>
35+
{React.string("Hello ReScripters!")}
36+
</div>
37+
}
38+
```
39+
```js
40+
var React = require("react");
41+
42+
function Greeting(Props) {
43+
return React.createElement("div", undefined, "Hello ReScripters!");
44+
}
45+
46+
var make = Greeting;
47+
```
48+
49+
</CodeTab>
50+
51+
**Important:** Always make sure to name your component function `make` and don't forget to add the `@react.component` attribute.
52+
53+
We've created a `Greeting.res` file that contains a `make` function that doesn't receive any props (the function doesn't receive any parameters), and returns a `React.element` that represents `<div> Hello ReScripters! </div>` in the rendered DOM.
54+
55+
You can also see in the the JS output that the function we created was directly translated into the pure JS version of a ReactJS component. Note how a `<div>` transforms into a `React.createElement("div",...)` call in JavaScript.
56+
57+
## Defining Props
58+
59+
In ReactJS, props are usually described as a single `props` object. In ReScript, we use [labeled arguments](docs/manual/latest/function#labeled-arguments) to define our props parameters instead. Here's an example:
60+
61+
<CodeTab labels={["ReScript", "JS Output"]}>
62+
63+
```res
64+
// src/Article.res
65+
@react.component
66+
let make = (~title: string, ~visitorCount: int, ~children: React.element) => {
67+
let visitorCountMsg = "You are visitor number: " ++ Belt.Int.toString(visitorCount);
68+
<div>
69+
<div> {React.string(title)} </div>
70+
<div> {React.string(vistorCount)} </div>
71+
children
72+
</div>
73+
}
74+
```
75+
```js
76+
var React = require("react");
77+
78+
function Article(Props) {
79+
var title = Props.title;
80+
var visitorCount = Props.visitorCount;
81+
var children = Props.children;
82+
var visitorCountMsg = "You are visitor number: " + String(visitorCount);
83+
return React.createElement("div", undefined, React.createElement("div", undefined, title), React.createElement("div", undefined, visitorCountMsg), children);
84+
}
85+
86+
var make = Article;
87+
```
88+
89+
</CodeTab>
90+
91+
### Optional Props
92+
93+
### Type Inference
94+
95+
The ReScript type system excels at computing the actual prop types through usage. We recommend to explicitly type your props (especially for exported components) to help your coworkers understand the types. For simple cases or experimentation, it's still fine to omit them:
96+
97+
98+
```
99+
// Button.res
100+
101+
@react.component
102+
let make = (~onClick, ~msg, ~children) => {
103+
<div onClick>
104+
{React.string(msg)}
105+
children
106+
</div>
107+
}
108+
```
109+
110+
In the example above, `onClick` will be inferred as `ReactEvent.Mouse.t => unit`, `msg` as `string` and `children` as `React.element`. Type inference is very useful to ommit a lot of annoying type annotations, especially for private functions that pass around values to other functions.
111+
112+
113+
## Using Components in JSX
114+
115+
Every ReScript component can be used in JSX. For example, if we want to use our `Greeting` component within our `App` component, we can do this:
116+
117+
<CodeTab labels={["ReScript", "JS Output"]}>
118+
119+
```res
120+
// src/App.re
121+
122+
@react.component
123+
let make = () => {
124+
<div>
125+
<Greeting/>
126+
</div>
127+
}
128+
```
129+
```js
130+
var React = require("react");
131+
var Greeting = require("./Greeting.js")
132+
133+
function App(Props) {
134+
return React.createElement("div", undefined, React.createElement(Greeting.make, {}));
135+
}
136+
137+
var make = App;
138+
```
139+
140+
</CodeTab>
141+
142+
**Note:** React components are capitalized; primitive DOM elements like `div` or `button` are uncapitalized. More infos on the JSX specifics and code transformations can be found in our [JSX section](/docs/manual/latest/jsx#capitalized-tag) in our language manual.
143+
144+
145+
## Submodule Components
146+
147+
We can also represent React components as submodules, which makes it very convenient to build more complex UI without the need to create multiple files for each composite component (that's probably only used by the parent component anyways):
148+
149+
```res
150+
// src/Button.res
151+
module Label = {
152+
@react.component
153+
let make = (~title: string) => {
154+
<div className="myLabel"> {React.string(title)} </div>
155+
}
156+
}
157+
158+
@react.component
159+
let make = (~children) => {
160+
<div>
161+
<Label title="Getting Started" />
162+
children
163+
</div>
164+
}
165+
```
166+
167+
The `Button.res` file defined in above is now containing a `Label` component, that can also be used by other components, either by writing the fully qualified module name (`<Button.Label title="My Button"/>`) or by using a module alias to shortcut the full qualifier:
168+
169+
170+
```res
171+
module Label = Button.Label
172+
173+
let content = <Label title="Test"/>
174+
```
175+
176+
177+
## Tips & Tricks
178+
179+
- Start with one component file and utilize submodule components as your component grows. Consider splitting up in multiple files when really necessary.
180+
- Keep your directory hierarchy flat. Instead of `article/Header.res` use `ArticleHeader.res` etc. Filenames are unique across the codebase, so filenames tend to be very specific `ArticleUserHeaderCard.res`, which is not necessarily a bad thing, since it clearly expresses the intent of the component within its name, and makes it also very easy to find, match and refactor across the whole codebase.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
---
2+
title: Installation
3+
description: "Installation and Setup"
4+
canonical: "/docs/react/latest/installation"
5+
category: Overview
6+
---
7+
8+
# Installation
9+
10+
Add following dependency to your ReScript project (in case you don't have any project yet, check out the [installation instructions](/docs/manual/latest/installation) in the manual):
11+
12+
```
13+
npm install reason-react --save
14+
```
15+
16+
Then add the following setting to your existing `bsconfig.json`:
17+
18+
```json
19+
{
20+
"reason": { "react-jsx": 3 },
21+
"bs-dependencies": ["reason-react"]
22+
}
23+
```
24+
25+
To test your setup, create a new `.res` file in your source directory and add the following code:
26+
27+
```res
28+
// src/Test.res
29+
@react.component
30+
let make = () => {
31+
<div> {React.string("Hello World")} </div>
32+
}
33+
```
34+
35+
Now rerun your build with `bsb -make-world` and you should see a successful build.
36+
37+
## Exposed Modules
38+
39+
After a successful installation, ReasonReact will make following modules available in your project's global scope:
40+
41+
- `React`: Bindings to React
42+
- `ReactDOM`: Bindings to the ReactDOM
43+
- `ReactDOMServer`: Bindings to the ReactDOMServer
44+
- `ReactEvent`: Bindings to React's synthetic events
45+
- `ReactDOMStyle`: Bindings to the inline style API
46+
- `ReasonReactRouter`: A simple, yet fully featured router with minimal memory allocations
47+
48+
49+
### Deprecated Modules
50+
51+
ReasonReact has gone a long way, so we still keep around a few modules for legacy reasons. We will deprecate them in the future in favor of...
52+
53+
- `ReasonReact` -> `React`
54+
- `ReactDOMRe` -> `ReactDOM`
55+
- `ReactDOMServerRe` -> `ReactDOMServer`
56+
- `ReasonReactCompat` -> no replacement needed
57+
58+
Actual usage and for each module will be discussed in the rest of this documentation.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
---
2+
title: Introduction
3+
description: "Introduction to ReScript & ReactJS"
4+
canonical: "/docs/react/latest/introduction"
5+
category: Overview
6+
---
7+
8+
# Introduction
9+
10+
ReScript offers first class bindings for [ReactJS](https://reactjs.org) and are designed and built by people using Reason and React in large, mission critical production React codebases. The bindings are compatible with all React versions > 16.8 (the version that initially introduced React hooks).
11+
12+
The ReScript philosophy is to compile as closely to idiomatic JS code as possible; in case of ReactJS we made no exception, so it's not only easy to transfer all the React knowledge to the ReScript platform, but also straightorward to integrate with existing ReactJS codebases and libraries.
13+
14+
> **This documentation assumes basic knowledge about ReactJS.**
15+
>
16+
> Please note that even though we will cover many basic React concepts, it might still be necessary to have a look at the official [ReactJS](https://reactjs.org) resources, especially if you are a complete beginner with React.
17+
18+
<Warn>
19+
20+
**Note about the wording:**
21+
22+
The React bindings are officially called **ReasonReact** due to legacy reasons. The name will eventually be adapted in the future. For simplicity reasons, we try to stick either to `ReScript & React`, `React bindings` or just `React`.
23+
24+
</Warn>
25+
26+
## Feature Overview
27+
28+
- No Babel plugins needed (JSX is part of the language!)
29+
- Bindings for all important React APIs needed to build production ready apps (`useState`, `useReducer`, `useEffect`, `useRef`,...)
30+
- No class based component API legacy (all ReasonReact codebases are built on functional components & hooks)
31+
- Strong level of type safetiness and type inference for component props and state values
32+
- [GenType](/docs/gentype/latest) support for importing / exporting React components in Flow and TypeScript codebases
33+
34+
## Development
35+
36+
- In case you are having any issues or if you want to help us out improving our bindings, check out our [ReasonReact Github repository](https://github.com/reasonml/reason-react).
37+
- For doc related issues, please go to the [rescript-lang.org repo](https://github.com/reason-association/rescript-lang.org).

0 commit comments

Comments
 (0)