-
Notifications
You must be signed in to change notification settings - Fork 4
/
ChangeUsername.js
44 lines (36 loc) · 1.01 KB
/
ChangeUsername.js
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
// React is loaded and is available as React and ReactDOM
// imports should NOT be used
class Username extends React.Component {
state = { value: "" };
changeValue(value) {
this.setState({ value });
}
render() {
const { value } = this.state;
return <h1>{value}</h1>;
}
}
function App() {
const nameRef = React.useRef();
const userNameRef = React.useRef();
function clickHandler() {
userNameRef.current.setState({ value: nameRef.current.value });
}
return (
<div>
<button onClick={clickHandler}>Change Username</button>
<input type="text" ref={nameRef} />
<Username ref={userNameRef} />
</div>
);
}
document.body.innerHTML = "<div id='root'></div>";
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(<App />);
setTimeout(() => {
document.querySelector("input").value = "John Doe";
document.querySelector("button").click();
setTimeout(() => {
console.log(document.getElementById("root").innerHTML);
}, 300);
}, 300);