English 中文(简体)
React: Array of refs or protocol in ref. Current
原标题:React: Array of refs or array in ref.current

在与许多方面合作时,我曾试图把我的头.倒。 目前,有两种办法储存多笔回馈。

射线

[useRef(null), useRef(null), ...]

或目前阵列。

useRef([])

我在阅读了这套书,但就这个专题做了一些研究。 两者都奏效,但应当使用什么? 也许后一种选择较好,因为一般规则是避免过度使用?

最佳回答

我处理许多问题的方法,请你参考:

const self = useRef({}).current
const saveRef = key => r => { self[key] = r }

return (
  <div ref={saveRef("container")}>
    <input ref={saveRef("input")} />
  </div>
)
问题回答

正如你所说的那样,我不认为对此有明确答案。 换文中提到了一种可变的价值,因此,我猜测这取决于你认为 mu价值是什么。 如果您的阵列出现突变,那么在使用Ref中储存阵列是有意义的。 如果您的数值清单有所变动,那么每值就会产生反射。 然而,我认为这确实是个人的偏好。

一种谨慎的词;对 re的 value价值进行更新,不会迫使重新投标。 我假定你知道这一点,因为你已清楚地读到。 The docs recent, but consider I would only calls out.

EDIT:同样,正如你所说的那样,你应当避免过度使用,如果你需要一整座大楼,你可能想考虑重新计算,以减少每部分的回馈数量。 我非常关心你需要这么多回想的用词。

同样,

const refs = useRef(Array.from({ length: numberOfElements }, () => React.createRef()));

// Example of accessing the first ref
const firstRef = refs.current[0];

采用这种办法,所有回馈都储存在<代码>ref. 现行财产内的一个单阵列中。 当你想一提管理所有<代码>refs时,这是有用的。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签