Simple, 0 dependency class-based React view model for when you can't give up OOP

15 comments · 33% upvoted · source · other discussions

comments

jax024

I laughed at the scenario because any dev with 20 yoe should be able to manage switching contexts between OOP and more functional patterns with ease.

cant_have_nicethings

React is the “latest and greatest”. It’s going to be 12 soon.

murden6562

This 1000%

macrozone13

This must be satire, right? RIGHT?

--think

20 Years experience???

volivav

But this is just useState with extra steps...

Not all code is best described declaratively.

It would've been nice if you had shown an example that wasn't a setState(current => current + 1)

_Pho_

The point of a VM is that you can put all sorts of logic in it. You could call an API from it and update the state of the class which then syncs to React. It's not world endingly cool but a lot of ppl are missing the point

stfuandkissmyturtle

Id hate working with this guy

bennett-dev [OP]

why

stfuandkissmyturtle

I might have been a bit mean there. But trust me. 60 percent of my work right now is cleaning up mess like this created by some backend dev who thinks frontend is just a dumb after thought.

React contrary to popular belief is very simple. Pulling shit like this doesn't only make it difficult to maintain but can introduce bugs and behaviors nobody thought about.

Just yesterday I had to fix dumb shit like

<div>

{getFooterComponent(date, links)}

</div>

They too had 20 years of experience . Put up a fight and had to finally point out the docs to them

https://react.dev/reference/rules/react-calls-components-and-hooks#never-call-component-functions-directly

So for me it comes from a place of frustration, you don't wanna do frontend don't do it.

mittyhands

jesus_christ_how_horrifying.jpg

a_reply_to_a_post

you can just...use keys

if something isn't rerendering when the value is updating, throw a key on it comprised of the updated value which will cause that component and it's children to rerender

svish

*remount

_Pho_

This seems... bad and hacky?

a_reply_to_a_post

well if something isn't rerendering properly when it should, it's probably how the application is architected, but instead of introducing a whole new view model as a work around as this poster is suggesting, a key on a component marks it as being something to rerender/remount and what keys are actually designed for

if your key doesn't change, your component doesn't change

it's actually a common pattern in react, using keys to force revalidations, especially if you've worked with any of the query libraries