react项目中,想往父组件Object-Container传递一个模块,请问怎么传递?

Object-Container.js

import React from 'react';

/**
 * 描述:Echarts 外层构造组件
 * 时间:2017年12月2日
 */

class Foundation extends React.Component {

    constructor() {
        super();
    }

    _init() {
        // 参数设置
        var doc = document.getElementsByClassName('cs001')[0]
        //var optionECharts = this.props.defaultProps;

    }

    componentDidMount() {
        this._init()
    }


    componentWillMount() {
        //this.showJson();
    }

    render() {
        return (
            <div className={'cs001'} style={{width: 800, height: 800}}>

            </div>
        )
    }
}

export default Foundation;

Object-01.js

import React from 'react';
import Foundation from './Object-Container';
import echarts from 'echarts';

class AbbottTest extends React.Component {
    constructor() {
        super();

        this.state = {
            width: 800,
            height:800,
            isRed: true,
            className01: 'data-ec1',
            classPosition01:{

            },
            className02: 'data-ec2',
            classPosition02:{

            },
            optionECharts: {
                new echarts
            }
        }


    }



    render() {
        var divStyle ={
            display: 'block',
            width: 500,
            height: 500
        }

        return (

            <div style={divStyle} className='data-line'>
                <Foundation defaultProps={this.state.optionECharts}
                />
            </div>


        )
    }
}

export default AbbottTest;
阅读 1.9k
1 个回答

props.children

<div>
    {this.props.children}
</div>

<AbbottTest>
    <Foundation />
</AbbottTest>

this.props.chilren就像当与 那个内部的 <Foundation />了

反正React16之前是可以这样写, 好像16之后你可以用Portal。 Portal我也没用过。

不知道你的defaultProps还有没有效,我知道可以这样写

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题