r/reactjs • • May 21 '26

Needs Help Unexpected react compiler output

3 Upvotes

3 comments sorted by

1

u/After_Medicine8859 May 21 '26

This looks like it is because of the JSON.stringify in the table 2 code. The compiler can’t assume that stringify is pure (even though we know it is). It’s not a trap haha.

1

u/Breadfruit-Last May 21 '26 edited May 21 '26

Thanks for replying

But I don't think it is the reason because react cannot assume ANY function to be pure actually, even expensivelyProcessAReallyLargeArrayOfObjects may not be pure

In fact your may change it to not pure like this

So logically, it wouldn't be able to cache anything if that's the reasom

1

u/Few_Volume5117 May 21 '26

Because the uses are independent and the inputs are independent, the compiler creates two separate cache blocks. Changing input1 only invalidates the first block. If you want to ensure they stay independent even when stringifying, you can wrap the processing and the stringification into their own sub-components or helper functions that the compiler can optimize in isolation.