I came across the Component Composition Design Pattern in React, which is said by the tutor to be analogue to inheritance in OOP. My question is, if I want to extend the defined props of the Button, how do you do this?!
SIDE NOTE: I know you would do this with CSS in the first place, but I ran outta ideas to customize the tutor's example.
In the second code snippet I tried adding both, borderColor: "blue" and color="red" to the style attribute of the BigSuccessButton to try different approaches of appending stuff.
But with the style attribute the entire content defined in the Button Component will be killed. So I will only see blue borders.
So I thought of adding a new prop and using it. But if last mentioned is the way to do this, how can I append this thing?
Those are the Composition Components, with Button being the Super Class:
export const Button = ({ size, bgColor, text, ...props }) => {
console.log(props);
return (
<button
style={{
padding: size === "large" ? "32px" : "8px",
fontSize: size === "large" ? "32px" : "16px",
backgroundColor: bgColor,
}}
{...props}
>
{text}
</button>
);
};
export const DangerButton = (props) => {
return <Button {...props} bgColor="red" />;
};
export const BigSuccessButton = (props) => {
return (
<Button
{...props}
size="large"
bgColor="green"
/>
);
};
Here I wanna add text color to BigSuccessButton:
import { BigSuccessButton, DangerButton } from "./Composition";
function App_FuncProg() {
return (
<>
{/* <RecursiveComponent data={nestedObject} /> */}
<DangerButton text="Danger" />
<BigSuccessButton text="Yippieh!" style={{borderColor: "blue"}} color="red" />
</>
);
}
export default App_FuncProg;