-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathHelloReactSurface.js
More file actions
48 lines (42 loc) · 1.18 KB
/
HelloReactSurface.js
File metadata and controls
48 lines (42 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import React, { useEffect, useState } from "react";
import { useLiveContext } from "react-surface";
// props are provided by the server.
const HelloReactSurface = (props) => {
const [count, setCount] = useState(0);
const { pushEvent, handleEvent, pushEventTo } = useLiveContext();
useEffect(() => {
handleEvent("from_server", (val) => {
console.log("from server!");
console.log(val);
});
}, []);
const incCount = () => {
setCount((c) => c + 1);
};
const handleSubmit = (e) => {
e.preventDefault();
const data = new FormData(e.target);
pushEvent("update_name", { new_name: data.get("name") });
};
return (
<div>
<h3>Props:</h3>
<div>{JSON.stringify(props)}</div>
<br />
<form onSubmit={handleSubmit}>
<input type="text" name="name" placeholder="new name" />
<button type="submit"> Update Name </button>
</form>
<div>
<p>Count: {count}</p>
<button type="button" onClick={incCount}>
Inc
</button>
</div>
<button type="button" phx-click="trigger-event">
Trigger Server Event
</button>
</div>
);
};
export default HelloReactSurface