DEV Community

Ding Fan
Ding Fan

Posted on

An interesting JS question

You are refactoring a function renderView which takes an item as parameter. In renderView there is a setState function which will trigger a render process.

let renderView = (item) => {
    // some logic play with item
Enter fullscreen mode Exit fullscreen mode

Currently, function renderView was called too many times in a business action, thus setState was triggered again and again. You want to reduce the trigger counts to improve the performance. You are trying to make function renderView takes an array of items, so you can call setState only once after deal with all the objects.

After some attempts, you found that the renderView was appeared every where in the code base, it's error-prone to change the parameter of renderView and every logic called it, you shouldn't touch those legacy code and of course you will never want to do it.

You decide to hack renderView to make it trigger only once while called several times in a row. But how?

Top comments (0)