I have a parent component and a child component,I want to pass
property from Parent to Child by using {…this.props},I dont want
any action or reducer in the picture,Is it possible to do this?
我的孩子组件是这样的: –
import React,{ Component } from 'react' import ReactDOM from 'react-dom' class SampleChild extends Component { constructor(props) { super(props) } render(){ return( <div>This is Parent</div> ) } } SampleChild.propTypes={ Header:React.PropTypes.string.isrequired } export default SampleChild
我的父组件是这样的: –
import React,{ Component } from 'react' import ReactDOM from 'react-dom' class SampleParent extends Component { constructor(props) { super(props) } render(){ return( <div><SampleChild {...this.props}/></div> ) } } export default SampleParent
现在我如何将SampleParent组件中的Header属性传递给SampleChild?.请帮助我.
<SampleParent Header="Hello from Parent" />
因为你将所有道具从SampleParent传播到SampleChild,你需要确保SampleParent只是接收它作为支柱,如果它是动态的,那么你会为你做这个技巧.
如果它是静态道具,您可以在DefaultProps中为SampleParent定义它,并且您将始终传递相同的字符串.
SampleParent.defaultProps = { Header: 'Hello from Parent' }