You can use spread operator to reduce code.
class TextInput extends React.Component {
constructor(props) {
super(props);
}
render() {
return <input {...this.props} />;
}
}
export default TextInput;
This way you even don't need a seperate PasswordInput component.
<TextInput id="name" type="text" placeholder="Your Name" />
<TextInput id="password" type="password" />
Answer from SuleymanSah on Stack OverflowMedium
medium.com › @rashmipatil24 › reusable-react-components-7b025f232ca4
A Guide to Building Reusable React Components
September 21, 2024 - When creating reusable components, it’s important to ensure that they can be styled consistently across your application. You can achieve this by using CSS classes, styled-components, or CSS-in-JS libraries like Emotion or Styled Components.
Is it not possible to create reusable components like React in templating languages
You absolutely can. I do with Django templating language. You simply use extend and include template tags. More on reddit.com
Is it always a good practice to do reusable components ?
It’s recommended but it depends the context and the responsibility of the components. Sometimes when you look for reusability you ends up creating excessive abstraction on your software components. More on reddit.com
Best practices for reusable forms
Like you said, You should definitely have a form state management abstraction of some kind, you really don’t want to be creating your own form state logic from scratch for every form. It’s not just better at scale, it’s better in every case. Building your own would be a fun exercise though, definitely a great learning experience. The beginning of building something like this is actually a custom hook that will manage the form state (errors and inputs). Then whatever is returned from that hook could get passed to your form input components where they could handle updating the text values etc Some people argue uncontrolled should be preferred until controlled is required. I think that just adds complexity for no good reason and always use controlled because it’s simpler to implement a non-terrible UX (input specific, validation step specific error messages) react-hook-form plus the zod resolver works really well once you decide to learn a library. More on reddit.com
what to do with non reusable components?
i would have a components folder for each page that contains the non-shared components. More on reddit.com
Videos
19:51
Become a Master at Reusable Components in React - YouTube
React Reusable Components: Simplify Development & Boost ...
10:35
Create Highly Reusable React Components in Minutes with TypeScript ...
12:47
Creating High-Quality React Components: Best Practices for ...
- YouTube
32:03
How to build a reusable react component library like a pro - Time ...
Top answer 1 of 2
3
You can use spread operator to reduce code.
class TextInput extends React.Component {
constructor(props) {
super(props);
}
render() {
return <input {...this.props} />;
}
}
export default TextInput;
This way you even don't need a seperate PasswordInput component.
<TextInput id="name" type="text" placeholder="Your Name" />
<TextInput id="password" type="password" />
2 of 2
1
Let's say your reusable component is named FormInput
The component definition should look like this:
const FormInput = (props: any) => {
const [value, setValue] = useState(props.value);
useEffect(() => {
setValue(props.value);
}, [props.value])
return (
<input
autoComplete={props.autoComplete}
name={props.name}
type={props.type}
id={props.id}
value={props.value}
data-index={props.index}
onChange={props.onChange}
disabled={props.disabled}
ariaLabelledBy={props.ariaLabelledBy}
required={props.required}/>
)
};
And in the page where you want to use it you can simply invoke the component pass all the props to the component and implement the onChange event handler on the page:
Let's say you want to invoke the Input component on Login page:
const LoginPage = (props: any) => {
... // you have some useEffect etc. that is related to the business logic of the page
const handleChange = (event: any) => {
// put your logic here
}
return (
... // other HTML elements/components
<FormInput type="text" onChange={handleChange} id="test" value="John Doe" ... />
)
};
Brockherion
brockherion.com › blog › building-reusable-components-in-react-with-typescript-and-generics
Building Reusable Components in React with Typescript and Generics — Brock Herion
In this article, I will be showing you how you can use Typescript generics to build reusable and more robust components for your React applications
GeeksforGeeks
geeksforgeeks.org › reactjs › how-to-make-reusable-react-components
How to make Reusable React Components ? - GeeksforGeeks
August 13, 2025 - Step 2: After creating your project folder(i.e. gfg), move to it by using the following command: ... Example: This below example demonstrate the Reusable React Components. In this example, You can see ProductList and ProductItem component can be composed together and accept props for customization and are reusable with different data sets. ... //File path: src/App.js import React from 'react'; import ProductList from './ProductList.js'; const App = () => { const products = [ { id: 1, name: 'Product 1', price: 10 }, { id: 2, name: 'Product 2', price: 20 }, { id: 3, name: 'Product 3', price: 30 }, ]; return ( <div style={{ margin: '5px' }}> <h2 style={{ color: 'green' }}> GeeksForGeeks | Reusable Components Example </h2> <ProductList products={products} /> </div> ); }; export default App;
Theodorus Clarence
theodorusclarence.com › blog › fully-reusable-components
How to Create Fully Reusable React Components | theodorusclarence.com
April 21, 2024 - While it is a good practice to spread all of the props, do note that not all component needs to be fully reusable. For example, a ProductList component that renders a list of products, probably only needs to be customizable in the className props. You can use Pick for that, and take only the className props. interface ProductListProps extends Pick<React.ComponentPropsWithoutRef<'div'>, 'className'> { product: Product; } // No need to spread since we only need className export default function ProductList({ className, products }: ProductListProps) { return ( <div className={cn(['grid-cols-3', className])}> {products.map((product, i) => ( <Card key={product.id} product={product} /> ))} </div> ); }
egghead.io
egghead.io › lessons › react-create-a-simple-reusable-react-component
Create a Simple Reusable React Component | egghead.io
[04:49] To review, to create a component that is reusable throughout your application and as composable as any other of the JSX that you have available to you is you create a function that has a capital letter as the first character.
Published September 26, 2017
freeCodeCamp
freecodecamp.org › news › how-to-create-a-truly-reusable-react-component-from-scratch
How to Create a Truly Reusable React Component from Scratch
August 20, 2021 - As we have seen in this tutorial, by creating a reusable InputControl component and using ref to manage each input textbox's suggestion separately, we're able to create a truly reusable component for showing autocomplete suggestions. You can find the complete source code for this tutorial in this repository and live demo here. If you want to learn Redux in detail from scratch and build 3 apps along with the complete food ordering app, check out the Mastering Redux course. ... How to use Redux in React using the react-redux library to make your app reactive.
SitePoint
sitepoint.com › blog › javascript › a practical guide to creating reusable react components
A Practical Guide to Creating Reusable React Components — SitePoint
February 19, 2024 - Therefore, building a flexible, reusable layout that can be utilized in many different pages is essential. This will save you very many lines of code and consequently a tremendous amount of time. Consider the following React functional component: // Feed.js import React from "react"; import style from "./Feed.module.css"; export default function Feed() { return ( <div className={style.FeedContainer}> <header className={style.FeedHeader}>Header</header> <main className={style.FeedMain}> { <div className={style.ItemList}> {itemData.map((item, idx) => ( <div key={idx} className={style.Item}> {item} </div> ))} </div> } </main> <footer className={style.FeedFooter}>Footer</footer> </div> ); } const itemData = [1, 2, 3, 4, 5];
Angular Minds
angularminds.com › blog › how-to-create-and-use-reusable-react-components
How to Create and Use Reusable React Components
March 21, 2024 - The React Context API enables developers to handle states at a higher level, providing a simple and effective solution for sharing global data between components. By creating reusable contexts, developers can streamline state management and address common challenges like theming or authentication with ease. ... // ThemeContext.js import { createContext, useState, useContext } from 'react'; const ThemeContext = createContext(); export const ThemeProvider = ({ children }) => { const [theme, setTheme] = useState('light'); const toggleTheme = () => { setTheme((prevTheme) => (prevTheme === 'light' ?