Email or username:

Password:

Forgot your password?
Darius Kazemi

I just spent 4 hours trying to do something the idiomatic React way, trying about 6 different implementations involving many lines of code changes.

Ultimately I gave up and wrote 2 lines of plain JS to fix the issue. Will it probably bite me in the ass later? Sure. Would the many lines of code changes that React wanted me to do ALSO have bitten me in the ass later? Also yes. At least this is a *legible* piece of inadvisable code.

8 comments | Expand all CWs
Darius Kazemi

Remember the DRY principle in web application development:

Don't React Yourself

alys

@darius the core ideas of React seem pretty solid but they don't always seem very well implemented.

Григорий Клюшников

It usually helps when you view every dependency your project has as a liability. With every dependency, you lose some flexibility. Sometimes this lost flexibility doesn't matter and saves you sanity — operating systems are a prime example, no one wants to go back to the DOS days when each game came with a bunch of sound card drivers and a memory manager and all that stuff. But abstracting away something as simple and as universal as the DOM API isn't going to do anyone any good. Also I can't understand what is this "state" that most everyone tries to somehow "manage" these days.

It usually helps when you view every dependency your project has as a liability. With every dependency, you lose some flexibility. Sometimes this lost flexibility doesn't matter and saves you sanity — operating systems are a prime example, no one wants to go back to the DOS days when each game came with a bunch of sound card drivers and a memory manager and all that stuff. But abstracting away something as simple and as universal as the DOM API isn't going to do anyone any good. Also I can't understand...

Darius Kazemi

@grishka Yeah, unfortunately this is a big project that predates me by many years at work so I am stuck with React.

Josh Lee

@darius would be very curious to hear what the attempt was

Darius Kazemi

@jleedev basically I gave components

A
B C
D

C needs to change how it behaves on click based on state of D. Raising state to A and passing down causes a rerender of the whole tree on state change, which I can't have. I tried 6 or so tricks including refs, usecallback, context, reducer & dispatch instead of state, etc. Nothing worked to prevent the rerender so now C just queries the DOM to see if D has a certain class.

Max

@darius

Idiomatic React would suggest if you are worried rerender times in the tree you may have something else possibly unrelated wrong (calls to a Class.render or functionComponent are expected to be fast because they may be called often in the virtual DOM approach). React generally airs on the side of caution that shared state is a full rerender: if the state change in A then yeah everything under A generally rerenders.

@jleedev

Go Up