Jay*_*Jay 1 dictionary reactjs react-hooks
我不知道如何在地图中添加参考。我尝试过这个东西,但是根本不起作用。
import React, { useRef } from "react";
import ReactDOM from "react-dom";
import "./styles.css";
function App() {
let numRef = useRef([]);
const mat = [1, 2, 3, 4, 5];
console.log(numRef); //
return (
<div className="App">
<div>
{mat.map((element, idx) => (
<p ref={numRef.current[idx]} type="text">
{element}
</p>
))}
</div>
</div>
);
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
Run Code Online (Sandbox Code Playgroud)
我想在每个p标签中提供所有不同的ref。
您必须在ref中有一个函数来分配ref
function App() {
let numRef = useRef([]);
const mat = [1, 2, 3, 4, 5];
console.log(numRef); //
return (
<div className="App">
<div>
{mat.map((element, idx) => (
<p ref={(ref) => numRef.current[idx] = ref} type="text">
{element}
</p>
))}
</div>
</div>
);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
39 次 |
| 最近记录: |