【发布时间】:2023-01-25 15:15:52
【问题描述】:
在不重新渲染的情况下,组件需要在屏幕上显示增量值(changes.current)。单击按钮时,会发生增量,它会显示在控制台上但不会更改屏幕上的值。
我们需要显示增量值(changes.current)。我也试过 document.getElementById("p_id") 但它不起作用。
import "./styles.css";
import React, { useRef } from "react";
export default function App() {
let changes = useRef(0);
const changingValue = () => {
changes.current++;
//To print on console
console.log("New Value: " + changes.current);
};
// Statement to check the rerendering happening or not
console.log("rendering not happening");
return (
<div className="App">
{/* Button for click */}
<button onClick={changingValue}>
<b>Click me</b>
</button>
<p id='p_id'>{changes.current}</p>
</div>
);
}
期待: 单击按钮时应显示更新的值: enter image description here`
【问题讨论】:
标签: javascript reactjs typescript react-hooks ref