[英]Can I remove document.getElementById in reactjs?
我可以在沒有的情況下更改道具value
嗎
document.getElementById(objRef.uid + "_size" ).value = val
請注意,這些下拉列表是在 for 循環中進行的,並且有數千個。
<DropDownMenu
id={objRef.uid + "_size"}
setValue={( val ) => { document.getElementById( objRef.uid + "_size" ).value = val }}
value={objRef.size}
colors={["bg-red-500", "bg-yellow-400", "bg-emerald-400"]}
options={["Small", "Medium", "Large"]} />
是的你可以。 您可以使用 useRef 掛鈎。
使用它的示例(您可以記住,每個 HTML 元素都有一個引用屬性,您可以在其中實現一個值,例如您看到一個輸入元素,但對於 div 和更多元素是相同的)
function App() {
const inputElement = useRef();
const focusInput = () => {
inputElement.current.focus();
};
return (
<>
<input type="text" ref={inputElement} />
<button onClick={focusInput}>Focus Input</button>
</>
);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.