import React, { Fragment } from 'react';
import root from './index';
import { useState, useEffect, useRef } from 'react';
export default function App() {
const [count, setCount] = useState(0);
const myRef = useRef();
function add() {
setCount(count => count + 1);
}
function unmount() {
root.unmount(document.querySelector('#root'));
}
function show() {
alert(myRef.current.value);
}
useEffect(() => {
let timer = setInterval(() => {
setCount(count => count + 1);
}, 1000);
return () => {
clearInterval(timer);
};
}, [count]);
return (
<Fragment key={1}>
<input type="text" ref={myRef} />
<h1>我被点击了{count}次</h1>
<button onClick={add}>点我</button>
<button onClick={unmount}>卸载组件</button>
<button onClick={show}>点我提示数据</button>
</Fragment>
);
}