-
Notifications
You must be signed in to change notification settings - Fork 47.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Proposal] Add component properties like children #25704
Comments
You can actually do this using Compound Components Design Pattern. There you have an small guide of how to implement it. That's one way to do it. You can use the Children to manipulate the children in your component and the cloneElement to change the props on every child Here is an example of an If/Else component (this pattern is not a good approach of how to do this. Is just for educational purposes): import { Children, cloneElement } from "react";
const If = ({ children, predicate }) => {
return Children.map(children, (child) => {
return cloneElement(child, { predicate });
});
};
const Then = ({ children, predicate }) => {
return predicate ? <>{children}</> : <></>;
};
const Else = ({ children, predicate }) => {
return !predicate ? <>{children}</> : <></>;
};
If.then = Then;
If.else = Else;
export default If; Look how I created two properties on the main component and assigned the proper component to it, so now you can just do something like: import If from "./components/If";
import { useState } from "react";
function App() {
const [random, setRandom] = useState(Math.random() * 100);
return (
<div
style={{ display: "flex", height: "inherit", flexDirection: "column" }}
>
<If predicate={random <= 50}>
<If.then>
<p style={{ color: random < 40 ? "blue" : "red" }}>I'm {random}</p>
</If.then>
<If.else>
<div>
<p style={{ color: random > 80 ? "green" : "violet" }}>
I'm {random}
</p>
<button onClick={() => setRandom(0)}>Reset</button>
</div>
</If.else>
</If>
<button
onClick={() => setRandom(Math.random() * 100)}
style={{ width: "50%" }}
>
Randomize
</button>
</div>
);
}
export default App; In that snippet I made the implementation where I just generate random numbers and if is equal or less than 50 I just render "I'm 34" (In case that random is 34). In other ways I render the same message but with a button to reset the random number to 0. I actually have this code with more components like Switch or a Counter using Compound Components Pattern in a public repository. I'll drop it right here Hope you find this helpful. Kindest Regards, zJaaal |
Very good, it is cool, but this way is more visual than functional. It is a way to make the code prettier, but we still need to create some components and it doesn't cover the need to direct the component to a specific position in the parent. I think the proposal is still relevant. |
What about reactjs/rfcs#223 |
It works the same way. very nice @nihgwu. It solves the problem with another approach. |
This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize it yet. If you have any new additional information, please include it with your comment! |
Closing this issue after a prolonged period of inactivity. If this issue is still present in the latest release, please create a new issue with up-to-date information. Thank you! |
I propose to add support to custom child properties. This expand the options to create and organize the components and can reduce the amount of components in some cases.
Current Behavior
Desired Behavior
This example above expand the options to create and organize the components.
To use a real case...
Some developers make their codes like this:
... creating two components (CardContent and CardActions) to direct the content...
(this example was copied from https://mui.com/pt/material-ui/react-card/#OutlinedCard.tsx and changed to simplify)
... but imagine doing something like this:
eliminating the need to create more components, reducing the amount of components on the page.
So, adding support to use the components properties, like header; body and footer from MyComponent in my example, as a children make possible this behaviors.
make sense?
The text was updated successfully, but these errors were encountered: