by tekstar · 5y ago · 0 comments
I use it 2014-style. Class-based components, componentWillMount, render, callbacks, and that's about it.
I love this workflow, and generally structure all my UIs this way regardless of platform - have a state struct and then fully render the UI as a function of the state. To me, that's the big hurrah for using React.
But I have no clue why React is at version 18 and why it keeps getting new features. I'm perfectly happy making quite complicated UIs with the pattern I know well. Should I be following along? Does it matter?
You basically stick to writing pure functional components focused only on how to render, and try to abstract reusable logic into hooks, and expose only what you need
For example, you could make a useApi() hook and inside your component just do const { data, loading, error } = useApi('/endpoint'), so you can hide everything you don't need away from the rendering logic
If you’re using hooks like useState or useEffect I wouldn’t consider those functional components pure.
Hooks provide some nice syntactic sugar, which is not nothing, and make it easier to share logic between components. In my experience hooks have made my code easier to read, but they haven’t simplified it. The side effects and state might not be inlined but they’re still there. If anything they’ve made it less explicit because some of the details are hidden behind the magic that the React runtime uses to add state to functions.
And if you really wanted to make it 'pure', you could easily write a hook with an explicit return type that wraps the type of the initial value. If you're using typescript, you can then limit the possible types of the generic to only monads.
type State<T> = val as T;
const useStatex = <T>(initialState: T) => {
const [state, setState] = useState(initialState);
return [state, updateState] as [State<T>, (val: T) => void];
}
This new hook, useStatex, has an explicit type definition that indicates something stateful is taking place. Same as Futures. You can take this even further in the typing limiting the generic to only allow certain types for the underlying values to be monads, or even more simply type State<T> = Promise<T> | Array<T> | Option<T>
So you avoid an extra layer of nesting like State<Future<Array<T>>>
Point is, there are lots of ways to easily resolve any its not pure! arguments same as any other functional programming concepteta
The simplest way to limit the return type might even be to just do this
const useStatex = <T extends Promise | Array | Option>(initialState: T) => {But the beauty of hooks is as you say reusable statefull behavior. It also lets you treat your functional components more like a pure function in my opinion.
If the hooks you use are poorly designed, then yeah, it may be more difficult. But well designed and implemented hooks make your component appear more like a pure function.
The state is (more) decoupled from the UI code than before and lets me easily reuse behaviors across components.
Just FYI, once you add hooks, it is neither pure nor functional.
https://mckoder.medium.com/why-react-is-not-functional-b1ed1...
[1] Mostly useless aside: using a .then()able based plumbing would have opened up the possibility of using the async/await combinator language. The names async/await would give the wrong impression especially prior to the actual concurrency changes to eventually ship in React 18, but would have potentially been a more monadic fit.
And monads are impure [1]. If all your functions are impure then you're not doing functional programming.
[1] https://alvinalexander.com/scala/io-monad-doesnt-make-functi...
The useState hooks are a tiny bit clearer than the setState semantics. The effect hooks are less clear, but all told, the new mechanisms for handle global state and side effects better without Redux. (Redux is neat, but after working with it for years, I just have to conclude that in most cases it's just too much mental overhead compared to a more idiomatically Javascript solution.)
There's nothing wrong with continuing to use class-based components, but I think you'll find that everybody else is going to gradually deprecate it. The key facts about the work flow -- state to render function to surprisingly efficient DOM reconciliation -- remains the same.
https://www.reactiflux.com/transcripts/jordan-walke
> So I continued to explore framework-izing these ideas, and I had implemented several iterations of what eventually became React, in a few languages - one of the first explorations began as a rough implementation of the reconciler and component model in Standard ML (CreateClass was a "module function"). This was really great because SML embraces immutability by default which is natural when building React style components. We naturally wanted to deploy UI to web browsers, and at the time, the compile-to-JS landscape was not as mature as it is now - I don't even think source maps existed yet. So it made sense to port that exploration to JavaScript (...)
> One thing I noticed that when people were creating point to point bindings in their more traditional "MVC" app structure, it would almost always end up requiring "computable" bindings which invoke a function anytime a mutable cell had received a "change event". All these "computable bindings" ended up chaining together and small changes would end up causing large recomputation of the majority of the UI. I realized that functions already do transform inputs into outputs, and if we could just find a way to reinvoke those functions repeatedly, and quickly enough, that we could be much more expressive and concise, at not much more performance cost that the chain reaction that "computed bindings" would result in anyways.
If this isn’t you I’m sure at least the general performance improvements will be nice otherwise I don’t see why you can’t just keep doing what you’re doing
The communication about the "why" of new features has been extremely clear. E.g. for hooks: https://reactjs.org/docs/hooks-intro.html
Thanks for the link, I remember when Hooks came out but I didn't feel the need to adopt them, I'll take a look again.
I am asking because I do not remember them being used back then, so wondering if you had upgraded there.
Also what do you use for 'whole app' state handling (rather than component level) ?
Asking because React Context feature seems to be the right fit for the 'whole app' state handling, but it was not there back in 2014.
I also like you picked up preact for smaller project.. just because the size is so small. But for lager things (eg over 20K lines of JS) I seem to be staying with React.