用props子组件和父组件交互

子组件:

import React from 'react';
let Course = (props) => {
    return  (

我是{props.title}课程哦{props.count}

{props.children}

); } export default Course;

父组件:

import React, { Component } from 'react';

class Student extends Component {

    state = {
        showTime: false
    }


    xinashitime = () => {
        this.setState({
            showTime: !this.state.showTime
        }
        )
    }

    render() {

        console.log("触发了stdent")
        let myzujian = null;
        console.log("显示状态:" + this.state.showTime)
        if (this.state.showTime) {
            myzujian = (
                

你出来就舒服了

) } return (
我的标题是:{this.props.title}
{myzujian}
) } } let Teacher = (props) => { return (
老师好:{props.name}
) } export default Student; export { Teacher };

index.js

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';

ReactDOM.render(, document.getElementById('root'));

// If you want your app to work offline and load faster, you can change
// unregister() to register() below. Note this comes with some pitfalls.
// Learn more about service workers: https://bit.ly/CRA-PWA
serviceWorker.unregister();

 

你可能感兴趣的:(学习,react)