-
-
Notifications
You must be signed in to change notification settings - Fork 248
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial attempt to add something about events
- Loading branch information
Showing
1 changed file
with
37 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
--- | ||
title: Events | ||
description: "Event handlers for React elements" | ||
canonical: "/docs/react/latest/events" | ||
--- | ||
|
||
# Events | ||
|
||
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. | ||
|
||
## Capture the input value onChange | ||
|
||
Depending on the event handler, the callback function will have a different type. | ||
Due to the dynamic nature of JavaScript, we cannot anticipate the target type on the event. | ||
So, we need a leap of faith to grab the input value as string. | ||
|
||
```res | ||
module App = { | ||
@react.component | ||
let make = () => { | ||
let (value, setValue) = React.useState(_ => "") | ||
<form> | ||
<input | ||
type_="text" | ||
defaultValue={value} | ||
onChange={(ev: JsxEvent.Form.t) => { | ||
let target = JsxEvent.Form.target(ev) | ||
let value: string = target["value"] | ||
setValue(_prevValue => value) | ||
}} | ||
/> | ||
<p style={{color:"red"}}>{React.string(value)}</p> | ||
</form> | ||
} | ||
} | ||
``` |