π Key Concepts
What are reusable React components? You can think of them as building blocks.
They are independent pieces of code that c...
For further actions, you may consider blocking this person and/or reporting abuse
Very good write-up! One thing I would add in terms of reusability is render props. For example, if you have a generic button that renders a logo and a label, you can maximize reusability with a
renderIcon
prop.Thanks for sharing. I tried my best to keep this article as simple as possible focusing purely on beginners.
There are more advanced concepts for reusable components in React. Most of the time, we use Hooks in React for building reusable comp. Actually,they were introduced as a way to write reusable and stateful logic in functional components without needing class components.
But I believe, beginners should understand the React fundamental concepts pretty clearly first before jumping into advanced topics. That is what the article is about.
Thanks for writing this great article. As you mentioned this is focused for beginners, may be, title also can include that information.
The discussion on why incorporating API calls directly into button components is a bad practice is insightful. The step-by-step solution presented, extracting side effects and passing them as props, showcases a thoughtful approach to handling complex scenarios, ensuring that components remain truly reusable.
The examples provided, especially those related to buttons and navigation bars, effectively illustrate how to design components for maximum reusability. The emphasis on passing each functionality as props allows for flexibility and adaptability, crucial in diverse web development scenarios.
Overall, a well-crafted and informative article! π
I appreciate your detailed feedback
Nice article! I would like to add:
className
or do you need completely headless components? Should dark mode be built in?<Button isDanger={true}>
prop, but it might be better to do<Button type="danger">
if you foresee multiple types being added in the future (also, having the props<Button isDanger isWarning />
does not make sense).Like I said, building reusable component is not an easy task and it takes some practise, but leads to better code quality down the line.
Great explanation. Its really helpful.
Yes! Great article on writing good reusable low level components. I just want to point out that you may not want to apply these same rules to all components in a project when they are higher in the tree because of excessive prop drilling (passing down props down the entire component tree). In terms of layering I think it should be something like this: app/root -> composable layout -> (... additional app structure, all composable) -> business logic (actions/fetch/side effects) -> reusable leafs.
I agree.
Nice demonstration. Thanks for sharing this.
Thanks for your feedback.
great article!
Great article! , i hope this article will help to all beginners including me.
thanks for sharing
good article the same applies to otoher frameworks like vue, angular.
yes.
Well explained thanks for sharing
Glad it helped.
Scenario: You have a reusable component say "ButtonSave" which take props be label or styles or even icon.
A situation where this button is used in 100 different pages, and now your boss tells you a change(add/remove/etc) on the button.
You have to apply those changes in 100 location.
What would be the best practices on that ?
// maybe create dedicated wrappers for each usecase?
Γtimo artigo :)