Skip to content

Commit ce60b29

Browse files
committed
a little bit of the "responding to events" section
1 parent 3c0b071 commit ce60b29

File tree

4 files changed

+63
-23
lines changed

4 files changed

+63
-23
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
button {
2+
margin-right: 10px;
3+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from reactpy import component, html
2+
3+
4+
# start
5+
@component
6+
def button():
7+
return html.button("I don't do anything")
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from reactpy import component, html
2+
3+
4+
@component
5+
def button():
6+
def handle_click(event):
7+
print("You clicked me!")
8+
9+
return html.button({"on_click": handle_click}, "Click me")
10+
11+
12+
13+
# end
14+
if __name__ == "__main__":
15+
from reactpy import run
16+
from reactpy.utils import _read_docs_css
17+
18+
@component
19+
def styled_app():
20+
return html._(html.style(_read_docs_css()), button())
21+
22+
run(styled_app)

docs/src/learn/responding-to-events.md

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ title: Responding to Events 🚧
66

77
<p class="intro" markdown>
88

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.
9+
React lets you add _event handlers_ to your PSX. Event handlers are your own functions that will be triggered in response to interactions like clicking, hovering, focusing form inputs, and so on.
1010

1111
</p>
1212

@@ -18,35 +18,43 @@ React lets you add _event handlers_ to your JSX. Event handlers are your own fun
1818

1919
## Adding event handlers
2020

21-
To add an event handler, you will first define a function and then [pass it as a prop](/learn/passing-props-to-a-component) to the appropriate JSX tag. For example, here is a button that doesn't do anything yet:
21+
To add an event handler, you will first define a function and then [pass it as a prop](../learn/passing-props-to-a-component.md) to the appropriate PSX tag. For example, here is a button that doesn't do anything yet:
2222

23-
```js
24-
export default function Button() {
25-
return <button>I don't do anything</button>;
26-
}
27-
```
23+
=== "app.py"
24+
25+
```python
26+
{% include "../../examples/python/responding_to_events/simple_button.py" start="# start" %}
27+
```
28+
29+
=== ":material-play: Run"
30+
31+
```python
32+
# TODO
33+
```
2834

2935
You can make it show a message when a user clicks by following these three steps:
3036

31-
1. Declare a function called `handleClick` _inside_ your `Button` component.
32-
2. Implement the logic inside that function (use `alert` to show the message).
33-
3. Add `on_click={handleClick}` to the `<button>` JSX.
37+
1. Declare a function called `handle_click` _inside_ your `#!python def button():` component.
38+
2. Implement the logic inside that function (use `print` to show the message).
39+
3. Add `on_click=handle_click` to the `html.button` PSX.
3440

35-
```js
36-
export default function Button() {
37-
function handleClick() {
38-
alert("You clicked me!");
39-
}
41+
=== "app.py"
4042

41-
return <button on_click={handleClick}>Click me</button>;
42-
}
43-
```
43+
```python
44+
{% include "../../examples/python/responding_to_events/simple_button_event.py" end="# end" %}
45+
```
4446

45-
```css
46-
button {
47-
margin-right: 10px;
48-
}
49-
```
47+
=== "styles.css"
48+
49+
```css
50+
{% include "../../examples/css/responding_to_events/simple_button_event.css" %}
51+
```
52+
53+
=== ":material-play: Run"
54+
55+
```python
56+
# TODO
57+
```
5058

5159
You defined the `handleClick` function and then [passed it as a prop](/learn/passing-props-to-a-component) to `<button>`. `handleClick` is an **event handler.** Event handler functions:
5260

0 commit comments

Comments
 (0)