src

리액트 함수형 컴포넌트에서 상속해결

escher_ 2021. 8. 9. 21:05

https://reactjs.org/docs/composition-vs-inheritance.html

 

Composition vs Inheritance – React

A JavaScript library for building user interfaces

reactjs.org

 

컴포넌트 안에 컴포넌트를 넣음으로 해결한다.

 

컴포넌트 사이에 위치한 컴포넌트는

prop.children

으로 받아와서 부모 컴포넌트에 넘겨준다.

 

 

const Parent = ({children}) => {
    
    // parent props & state
    // parent methods...

    return (
        <View>
            {children}
        </View>
    )
}

const Children = () => {
    return (
        <Parent>
            <Text>Hello React Native!</Text>
        </Parent>
    )
}

const App = () => {
    return (
        <Children />
    )
}