Last active
September 14, 2023 01:15
-
-
Save addisonschultz/23cc128ef97d9fc96c756c1d488076b5 to your computer and use it in GitHub Desktop.
Cloning React Children
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as React from "react"; | |
import { Frame, addPropertyControls, ControlType } from "framer"; | |
export function Component({ childComponent }) { | |
return ( | |
<div style={{ width: "100%" }}> | |
{React.Children.map(childComponent, (child, index) => { | |
return React.cloneElement(child, { | |
width: "100%", | |
key: index, | |
// Additional Props | |
}); | |
})} | |
</div> | |
); | |
} | |
addPropertyControls(Component, { | |
childComponent: { | |
type: ControlType.ComponentInstance, | |
title: "Child Component", | |
}, | |
}); |
@addisonschultz It was my mistake, I was trying to add the map
in the wrong place. This works great:
return (
<Frame
{...rest}
background={props.background}
visible={visible}
//onTap={onTap}
style={{
color: "#fff",
fontSize: 16,
fontWeight: 600,
}}
>
{React.Children.map(props.childComponent, (child, index) => {
return React.cloneElement(child, {
width: "100%",
key: index,
// Additional Props
})
})}
</Frame>
)
@addisonschultz I was wondering if there is a way to also manipulate properties of children contained inside linked components. For example if I link a frame with a text component inside. How can I manipulate the text value of that child component? Since in the example above the width
and key
are properties of the root linked component (frame).
Thanks for the reply. I'm trying to avoid overrides because they are slightly more difficult to explain to clients. I have achieved the above by doing this:
{React.Children.map(props.components, (child, index) => {
var childComp = React.Children.map(
child.props.children,
(item) => {
return React.cloneElement(item, {
text: props.text,
})
}
)
return React.cloneElement(child, {
width: "100%",
key: index,
children: childComp,
// Additional Props
})
})}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@netgfx are you still getting this? I'm not able to reproduce, but feel free to send me a file if you are still getting this