I
I
Ivan2021-02-18 16:38:41
React
Ivan, 2021-02-18 16:38:41

How to pass the children component to another component?

I have a Button component in react-native app

const Button = ({children, callback, ...props}) => {
    return (
        <MainButton {...props} onPress={callback}>
            <Text>
                {props.children}
            </Text>
        </MainButton>
    )
}


How can I pass content to it when I paste it in another component?

<Button
callback={() => setStatusQrCode(false)}
color={color_white}
background={color_blue}
weight={600}>
    {<Text>Продолжить</Text>}
</Button>


PS I can't find a pattern that shows how to pass any component as children

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander, 2021-02-18
@BenderIsGreat34

const Button = ({ children, callback, ...props }) => {
    return <MainButton {...props} onPress={callback}>{children}</MainButton>;
};

T
t800zippygod, 2021-02-18
@t800zippygod

So children contains all the components that you have registered inside the tag. Roughly speaking, the component tree.
If you want to pass all the components nested in it to MainButton -> Text in the button component, then you need to figure out not props.children there, but directly the children argument

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question