Skip to content

Commit 31dfa15

Browse files
committed
Initial attempt to add something about events
1 parent 2f91a1e commit 31dfa15

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

pages/docs/react/latest/events.mdx

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
---
2+
title: Events
3+
description: "Event handlers for React elements"
4+
canonical: "/docs/react/latest/events"
5+
---
6+
7+
# Events
8+
9+
React lets you add event handlers to your JSX. Event handlers are your own functions that will be triggered in response to interactions like clicking, hovering, focusing form inputs, and so on.
10+
11+
## Capture the input value onChange
12+
13+
Depending on the event handler, the callback function will have a different type.
14+
Due to the dynamic nature of JavaScript, we cannot anticipate the target type on the event.
15+
So, we need a leap of faith to grab the input value as string.
16+
17+
```res
18+
module App = {
19+
@react.component
20+
let make = () => {
21+
let (value, setValue) = React.useState(_ => "")
22+
23+
<form>
24+
<input
25+
type_="text"
26+
defaultValue={value}
27+
onChange={(ev: JsxEvent.Form.t) => {
28+
let target = JsxEvent.Form.target(ev)
29+
let value: string = target["value"]
30+
setValue(_prevValue => value)
31+
}}
32+
/>
33+
<p style={{color:"red"}}>{React.string(value)}</p>
34+
</form>
35+
}
36+
}
37+
```

0 commit comments

Comments
 (0)