📅  最后修改于: 2022-03-11 14:57:12.097000             🧑  作者: Mango
import React, { useRef, createRef, useState } from "react";
import ReactDOM from "react-dom";
function App() {
const [renderIndex, setRenderIndex] = useState(1);
const refFromUseRef = useRef();
const refFromCreateRef = createRef();
if (!refFromUseRef.current) {
refFromUseRef.current = renderIndex;
}
if (!refFromCreateRef.current) {
refFromCreateRef.current = renderIndex;
}
return (
Current render index: {renderIndex}
First render index remembered within refFromUseRef.current:
{refFromUseRef.current}
First render index unsuccessfully remembered within
refFromCreateRef.current:
{refFromCreateRef.current}
);
}
const rootElement = document.getElementById("root");
ReactDOM.render( , rootElement);