Earlier quoted context omitted.
I think React is both and when you describe it as either will depend on how you're using it. If you have a website with a couple of React components here and there, then you have React the library. If you have your app setup as a SPA, with react router, redux, react forms, maybe you started the app with createReactApp, etc, then you are using React the framework. I don't see how you can argue it's not a framework whe…
>If you have you're app setup as a SPA, with react router, redux, react forms, maybe you started the app with createReactApp, etc, then you are using React the framework react-router, redux etc. are all different libraries. Point still stands: You have to pick and assemble these different parts yourself. And then still all those libraries don't call into your code, but your code includes and wires them up.
Common mistakes writing React components with hooks
21–30 of 98 posts
Re: Common mistakes writing React components with hooks
#22Earlier quoted context omitted.
I think this is an important nitpick though in your defence. I have just started learning React having become pretty competent with Angular (day job plus some hybrid apps) and I have to say that I'm struggling to understand its popularity compared to Angular. Angular feels fully fleshed out, adheres to MVC mostly and has nice separation of html, css and the UI TS code. I come from a native coding background so this f…
I don't see why the CSS is mixed with the rest. It clearly isn't. Mixing JS and HTML doesn't seem problematic to me.
I've mixed plenty of html and js over the years and even html and php and it's nice to not have to anymore. Something about JSX rubs me the wrong way.
Re: Common mistakes writing React components with hooks
#23The first one just feels like a premature optimization. Yes calling setCount forces a rerender of that component, but unless there's lots of subcomponents inside that component, I wouldn't bother. Chances are later you'll need that state in the view, and if you have "unexpected side effects" from rerendering then that is the problem. The other tips are fine; effects should have a single responsibility and links and b…
Re: Common mistakes writing React components with hooks
#24I would argue that writing a React component with a hook is a mistake. There is usually an easier/clearer way to solve the problems that hooks are intended to solve with the existing React primitives.
Re: Common mistakes writing React components with hooks
#25Nitpick, but React is not a framework, it's a library. People miss this point often eg when they compare React to Angular
Re: Common mistakes writing React components with hooks
#26I would argue that writing a React component with a hook is a mistake. There is usually an easier/clearer way to solve the problems that hooks are intended to solve with the existing React primitives.
Exemples would be welcome here as I've a strong belief that hooks made react components cleaner / shorter
For simple components hooks may be fine. For more complex ones I prefer classes.
Re: Common mistakes writing React components with hooks
#27> useEffect(() => { > fetchData(); > updateBreadcrumbs(); > }, [location.pathname]); > There are two use cases, the "data-fetching" and "displaying breadcrumbs". Both are updated with an useEffect hook. This single useEffect hooks will run when the fetchData and updateBreadcrumbs functions or the location changes. Is this right? Wouldn't they only update when `location.pathname` changes? Also, there should be a whole…
a much bigger problem with code like this is the fetching of data gets much more complicated when the second parameter of useEffect is not just an empty array
Re: Common mistakes writing React components with hooks
#28Nitpick, but React is not a framework, it's a library. People miss this point often eg when they compare React to Angular
Moment.js is a library, you can use in any part of your app and it doesn't define how your application is going to work. You are in charge of the flow of the application. React pretty much defines how your application is going to work from the beginning. It can call itself a "library" but by any practical means, it isn't.
Let's say that if I want my own version of select2, I can develop it with react, though not easy.
Re: Common mistakes writing React components with hooks
#29Earlier quoted context omitted.
Exemples would be welcome here as I've a strong belief that hooks made react components cleaner / shorter
For one thing you may not use the ref attribute on function components because they don’t have instances. That means the component can't have a .focus() method for example. For simple components hooks may be fine. For more complex ones I prefer classes.
https://reactjs.org/docs/hooks-reference.html#useref
The example even uses .focus()
Re: Common mistakes writing React components with hooks
#30Nitpick, but React is not a framework, it's a library. People miss this point often eg when they compare React to Angular
>> but React is not a framework It's a framework which advertises itself as a library. In reality, there is nothing library-like about bypassing the DOM for rendering and making developers use a custom programming language (JSX)... and please don't give me this tired old argument that JSX is not compulsory! Has anyone ever seen a real React application which does not use JSX? Exactly. If 99.9999% of developers are us…
I've built one without using JSX and I think it's superior. The problem is the tooling and type support just isn't there so going against the grain here is pretty much asking to be burnt.