import React,{useState,useRef,createRef, useEffect,useMemo } from 'react';
function FocusInput1 (){
const inputElement = createRef();
const change = () =>{
console.log(inputElement.current)
inputElement.current.focus();
}
return(
<>
<input ref={inputElement} type="text"/>
<button onClick={change}>Focus input</button>
</>
)
}
function FocusInput (){
const inputElement = useRef();
const change = () =>{
console.log(inputElement.current)
inputElement.current.focus();
}
return(
<>
<input ref={inputElement} type="text"/>
<button onClick={change}>Focus input</button>
</>
)
}
function Test (){
const [renderId,setRenderId] = React.useState(1);
const refUseRef = useRef();
const refCreateRef = createRef();
console.log(refUseRef.current)
console.log(refCreateRef.current)
if(!refUseRef.current){
refUseRef.current = renderId;
}
if(!refCreateRef.current){
refCreateRef.current = renderId;
}
return(
<>
<p>Current render index: {renderId}</p>
<p>refUseRef:{refUseRef.current}</p>
<p>refCreateRef:{refCreateRef.current}</p>
<button onClick={()=>setRenderId(prev=>prev+1)}>
Cause re-render</button>
</>
)
}
function Test2(){
const [count,setCount] = useState(0);
function handleClick(){
setTimeout(()=>{
alert("Click on:" + count);
},3000)
}
return(
<>
{/**最终结果是 在count为6的时候, 点击 show alert ,
* 再继续增加 count , 弹出的值为 6, 而非 10.
* 为什么不是界面上 count 的实时状态?
* 当我们更新状态的时候,React 会重新渲染组件, 每一次渲染都会拿到独立的 count 状态, 并重新渲染一个 handleAlertClick 函数.
* 每一个 handleAlertClick 里面都有它自己的 count .
* 如何让点击的时候弹出实时的 count ?
* 看下一个例子
* */}
<p>click count:{count}</p>
<button onClick={()=>setCount(count+1)}>Click add</button>
<button onClick={handleClick}>alert</button>
</>
)
}
function Test3(){
const [count,setCount] = useState(0);
const latestCount = useRef(count);
console.log(latestCount.current)
useEffect(()=>{
latestCount.current = count;
console.log(count)
})
console.log(latestCount.current)
function handleClick(){
setTimeout(()=>{
alert("Click on:" + latestCount.current);
},3000)
}
return(
<>
{/**最终结果是 在count为6的时候, 点击 show alert ,
* 再继续增加 count , 弹出的值为 6, 而非 10.
* 为什么不是界面上 count 的实时状态?
* 当我们更新状态的时候,React 会重新渲染组件, 每一次渲染都会拿到独立的 count 状态, 并重新渲染一个 handleAlertClick 函数.
* 每一个 handleAlertClick 里面都有它自己的 count .
* 如何让点击的时候弹出实时的 count ?
* 看下一个例子
* */}
<p>latestCount:{latestCount.current}</p>
<p>click count:{count}</p>
<button onClick={()=>setCount(count+1)}>Click add</button>
<button onClick={handleClick}>alert</button>
</>
)
}
function Test4(){
const [count,setCount] = useState(0);
const preCountUseRef = useRef(count);
useEffect(()=>{
preCountUseRef.current = count;
})
return(
<>
<p>preCountUseRef:{preCountUseRef.current}</p>
<p>click count:{count}</p>
<button onClick={()=>setCount(count+1)}>Click add</button>
</>
)
}
const usePrevious = state =>{
const ref = useRef();
useEffect(()=>{
ref.current = state;
})
return ref.current;
}
function App(){
const [count,setCount] = useState(0);
const prevCount = usePrevious(count);
return(
<div>
<button onClick={()=>setCount(count+1)}>+</button>
<button onClick={()=>setCount(count-1)}>-</button>
<p>Now:{count},before:{prevCount}</p>
</div>
)
}
export default function MyInterval(){
const [count,setCount] = useState(0);
const doubleCount = useMemo(()=>{
return 2 * count;
},[count]);
const timerId = useRef();
useEffect(()=>{
timerId.current = setInterval(()=>{
setCount(count => count+1);
},1000);
},[])
useEffect(()=>{
if(count > 10){
clearInterval(timerId.current);
}
})
return(
<>
<button onClick={()=>setCount(count+1)}>
Count:{count},double:{doubleCount}
</button>
</>
)
}