react---父组件向子组件传值

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>父向子传</title>
        <script src="../lib/react.production.min.js" type="text/javascript" charset="utf-8"></script>
        <script src="../lib/react-dom.production.min.js"></script>
        <script src="../lib/browser.min.js"></script>
    </head>
    <body>
        <div id="out"></div>
    </body>
    <script type="text/babel">
        class App extends React.Component{
            constructor(props){
                super(props)
                this.state={
                    str:""
                }
            }
            tap(){
                this.setState({str:this.refs.ipt.value})
            }
            render(){
                return(
                    <div>
                            <h1>父传子</h1>
                            <input type="text"  ref="ipt"/>
                            <button onClick={this.tap.bind(this)}>传值</button>
                            <Box name={this.state.str}/>
                    </div>
                )
            }
        }
        
        class Box extends React.Component{
            constructor(props){
                super(props)
            }
            render(){
                return(
                <div>
                    <h1>子组件</h1>
                    <div>值--{this.props.name}</div>
                </div>
                )
            }
        }
        ReactDOM.render(<App />,document.getElementById('out'))
    </script>
</html>

猜你喜欢

转载自blog.csdn.net/weixin_43868692/article/details/86502642