import React, {useState, useEffect} from ‘react‘;
function Example() {
const [count, setCount] = useState(0);
useEffect(() => {
document.title = `You clicked ${count} times`;
});
return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>
Clickkk
</button>
</div>
);
}
export default Example


import React, { useState } from ‘react‘
const LoginControl = (props) => {
const [flag, setFlag] = useState(false)
const [cnt, setCnt] = useState(0)
const changeState = () => {
setFlag(state => {
return !state
})
setCnt(state => {
let newCnt = state
newCnt ++
return newCnt
})
}
let show = <h1 style={{ color: flag ? "red" : "yellow" }}>i see u</h1>
let button = <button onClick={changeState}>clickkkk</button>
return (
<>
{show}
{button}
<div>
<button onClick={() => setCnt(cnt + 1)}>numberrr</button>
<p>{cnt}</p>
</div>
</>
)
}
export default LoginControl

原文:https://www.cnblogs.com/zlrrrr/p/12008571.html