English 中文(简体)
使用心血
原标题:Use useMemo to meonize a function with parameter

我对使用国的怀疑是,它造成了许多复方和 app。 我在试图利用使用国的固定功能,这种回报有很多原因。

我发现,我可以使用Memo或使用Callbackhoo来宣传这一功能。 问题一是通过一个参数来履行这一职能。

我的法典中有些地方正在研究一个阵列,我希望并且希望把这一功能放在从 lo到处理数据的每一个阵列项目上。

const myComponent = () => {
    
   const [example, setExample] = useState({});

   const myFunction = (item) => {
      setExample(item)
   }

    return (
        array.map(item => {
          const test = myFunction(item);
        
          <p>{test}</P
        })
    )
 }

缩略语 自此以后,一度尝试了:

const test = useMemo(item => setExample(item), [setExample]);

但在我地图上打上<代码>测试(项目)时,它并不工作。

问题回答

这里,你如何写出接受参数的“回响”功能;

const myFunction = useCallback((item) => {
 // Your logic here
}, []); // Your useCallback deps here

这里是你所说的话;

myFunction(someItem)

而且,注意到你只应使用<代码>Memo。 当你想对归还结果(如阵列、物体等)进行回忆时。 当你想改变职能本身时,请使用<条码>。

首先,请谈谈<条码>使用Callback:它为包装使用<条码>中的功能而 s糖。

正常使用<代码> looks:

useMemo(() => n*n, [n]) // notice that it takes a function.

然后,如果你想总结一项职能,它就变成了。

useMemo(() => arg => { ... }, []) // notice that it takes a function.

<代码>Callback简单缩短了。

useCallback(arg => { ... }, []) // notice that it takes a function.

顺便提一下,你再次面临的问题是,你在职务的正文中宣布< myFunction。 它在每一方面都改变了身份(每次重新申报)。

like:

function MyComponent() {
   const myFunc = (arg) => { }
   useCallback(myFunc, [myFunc])
}

页: 1 每次都发生变动,而且没有使用附带要求。

为了使其发挥作用,你需要摘录myFunc:

const myFunc = () => { }
function MyComponent() {
   useCallback(myFunc, [myFunc])
}

如今,myFunc 具有稳定的特性,可在不同地点之间纪念。

因此,你的法典应当这样考虑:

const myFunction = (item, setExample) => { // Notice, we need to extract `setExample` into its own argument
   setExample(item)
}
const myComponent = () => {
    
   const [example, setExample] = useState({});

   const myMemoizedFunction = useCallback(myFunction, []) // `myFunction` never changes, it s a global constant

    return (
        array.map(item => {
          // This causes a rendering loop. Remove this!
          // const test = myMemoizedFunction(item, setExample);
        
          <p>{test}</P
        })
    )
 }




相关问题
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.

热门标签