React props vs children . What to use when?

后端 未结 5 467
伪装坚强ぢ
伪装坚强ぢ 2021-01-12 05:50

What is the difference between the two approaches ?

Passing text for a button as props

   

    function TextCo         


        
5条回答
  •  时光说笑
    2021-01-12 06:24

    As mentioned in the React official docs:

    Some components don’t know their children ahead of time. This is especially common for components like Sidebar or Dialog that represent generic “boxes”.

    We recommend that such components use the special children prop to pass children elements directly into their output:

    Simply put, it props.children just displays whatever is put between the opening and closing tags.

    As asked in your question, there is not much difference in the use case specified by you.

    But, say you had a small left icon the component then passing 'title' as a separate prop would make more sense Eg.

    
    
        function TextComponent(props){
            return (
                    
    ); }

提交回复
热议问题