在没有React-Router的React SPA中,如何在子组件中呈现父组件?

时间:2019-05-02 06:12:31

标签: reactjs components parent-child render

我关于堆栈溢出的第一个问题与与React的Child-Parent通信有关,更具体地说是如何调用父组件并将当前视图切换到parent-component-view。 我知道道具是从父母传给孩子的,孩子们可以通过调用父母的方法来要求父母通过道具。 但是,您如何称呼实体父组件,而不仅仅是道具。子组件调用要渲染的父组件。当前视图应消失。 由于可以导入组件,因此应该有一种方法可以仅在导入程序的组件中呈现它们。 我希望以下代码能说明我的问题。

import React from 'react';
import Parent from './parent';

class Child extends React.Component {
    constructor(props) {
        super(props);
    }
    this.changeView = this.changeView.bind(this);

    changeView() {
        return <Parent />;
    //or,  since we want to render:
    // React.render(<Parent />, document.getElementById('child-view');
    }

    render() {
        return (
        <div className="child-view">
            <div className="list">
                // content that should not be seen anymore
            </div>
            <button onClick={this.changeView}>
        </div>
        )
    }

0 个答案:

没有答案