react 高阶组件无法传值怎么办?想把BarAA的defaultProps传递给Foundation但是无法实现

import React from 'react'

import { Foundation } from './foundation'


class BarAA extends React.Component {

    constructor() {
        super();
        //localStorage.username='再见';
    }

    defaultProps = {
        autoPlay: false,
        maxLoops: 10
    }

    render() {

        return (
            <div>baraaaaaaa</div>
        )
    }
}

BarAA = Foundation(BarAA);

export default BarAA;

import React from 'react';

// HOC 工厂实现方法

export const Foundation = (WrappedComponent) => {

    class NewComponent extends React.Component {
        constructor() {
            super();
        }

        _init() {
            // 参数设置
            var doc = document.getElementById('ec');
            var optionECharts = {
                title: {
                    text: 'ECharts 入门测试开始'
                },
                tooltip: {},
                legend: {
                    data: ['销量']
                },
                xAxis: {
                    data: ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子", "无敌"]
                },
                yAxis: {},
                series: [{
                    name: '销量',
                    type: 'bar',
                    data: [5, 20, 36, 10, 10, 20, 90]
                }]
            };

            return import(/* webpackChunkName: "echarts" */ 'echarts').then(echarts => {
                // 基于准备好的dom,初始化echarts实例
                var myChart = echarts.init(document.getElementById('ec'));
                // 指定图表的配置项和数据
                var option = optionECharts;
                // 使用刚指定的配置项和数据显示图表。
                myChart.setOption(option);
            }).catch(error => 'An error occurred while loading the component');
        }



        componentDidMount() {
            console.log(this.defaultProps.maxLoops)
            this._init()
        }


        componentWillMount() {

        }

        render() {
            return (
                <div>
                    <WrappedComponent { ...this.props } />
                </div>
            )
        }
    }

    return NewComponent

}

export default Foundation;

图片描述

修改了一下,依然找不到那个属性

图片描述

import React from 'react'

import { Foundation } from './foundation'


class BarAA extends React.Component {

    constructor() {
        super();
        //localStorage.username='再见';
        this.state = {
            defaultProps: {
                autoPlay: false,
                maxLoops: 10
            }
        }
    }


    render() {

        return (
            <div>baraaaaaaa</div>
        )
    }
}

BarAA = Foundation(BarAA);

export default BarAA;

import React from 'react';

// HOC 工厂实现方法

export const Foundation = (WrappedComponent) => {

    class NewComponent extends React.Component {
        constructor() {
            super();
        }

        _init() {
            // 参数设置
            var doc = document.getElementById('ec');
            var optionECharts = {
                title: {
                    text: 'ECharts 入门测试开始'
                },
                tooltip: {},
                legend: {
                    data: ['销量']
                },
                xAxis: {
                    data: ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子", "无敌"]
                },
                yAxis: {},
                series: [{
                    name: '销量',
                    type: 'bar',
                    data: [5, 20, 36, 10, 10, 20, 90]
                }]
            };

            return import(/* webpackChunkName: "echarts" */ 'echarts').then(echarts => {
                // 基于准备好的dom,初始化echarts实例
                var myChart = echarts.init(document.getElementById('ec'));
                // 指定图表的配置项和数据
                var option = optionECharts;
                // 使用刚指定的配置项和数据显示图表。
                myChart.setOption(option);
            }).catch(error => 'An error occurred while loading the component');
        }

        showJson(){
            var test;
            if(window.XMLHttpRequest){
                test = new XMLHttpRequest();
            }else if(window.ActiveXObject){
                test = new window.ActiveXObject();
            }else{
                console.log("请升级至最新版本的浏览器");
            }
            if(test !=null){
                test.open("GET","./json.json",true);
                test.send(null);
                test.onreadystatechange=function(){
                    if(test.readyState==4&&test.status==200){
                        var obj = JSON.parse(test.responseText);
                        for (var name in obj){
                            console(obj[name].key);
                        }
                    }
                };

            }
        }

        componentDidMount() {

            this._init()
        }


        componentWillMount() {
            //this.showJson();
            console.log(this.state.defaultProps)
        }


        render() {
            return (
                <div>
                    <WrappedComponent/>
                </div>
            )
        }
    }

    return NewComponent

}

export default Foundation;
阅读 3.7k
3 个回答

这种不是用props传递的吗?

import React from 'react'

import { Foundation } from './foundation'


class BarAA extends React.Component {

    constructor() {
        super();     
        this.state = {
         defaultProps = {
        autoPlay: false,
        maxLoops: 10
    }
        }
    }
    render() {

        return (
            <div>
            <Foundation  defaultProps={this.state.defaultProps}/>
            </div>
        )
    }
}
export default BarAA;

<WrappedComponent { ...this.props } /> 你这里不用写啊,WrappedComnponent 本身就会有props,无需传递的。你本来就是default props

import React from 'react'

import { Foundation } from './foundation'


class BarAA extends React.Component {

    constructor() {
        super();
        //localStorage.username='再见';
        this.state = {
            optionECharts: {
                title: {
                    text: 'ECharts 开始我的世界'
                },
                tooltip: {},
                legend: {
                    data: ['销量']
                },
                xAxis: {
                    data: ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子", "无敌"]
                },
                yAxis: {},
                series: [{
                    name: '销量',
                    type: 'bar',
                    data: [5, 20, 36, 10, 10, 20, 90]
                }]
            }
        }
    }

    render() {

        return (
            <div>baraaaaaaa</div>
        )
    }
}

BarAA = Foundation(BarAA);

export default BarAA;

import React from 'react';

// HOC 工厂实现方法

export const Foundation = (WrappedComponent) => {

    class NewComponent extends WrappedComponent {
        constructor() {
            super();
        }

        _init() {
            // 参数设置
            var doc = document.getElementById('ec');
            /*
            var optionECharts = {
                title: {
                    text: 'ECharts 入门测试开始'
                },
                tooltip: {},
                legend: {
                    data: ['销量']
                },
                xAxis: {
                    data: ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子", "无敌"]
                },
                yAxis: {},
                series: [{
                    name: '销量',
                    type: 'bar',
                    data: [5, 20, 36, 10, 10, 20, 90]
                }]
            };
            */

            return import(/* webpackChunkName: "echarts" */ 'echarts').then(echarts => {
                // 基于准备好的dom,初始化echarts实例
                var myChart = echarts.init(document.getElementById('ec'));
                // 指定图表的配置项和数据
                var option = this.state.optionECharts;
                // 使用刚指定的配置项和数据显示图表。
                myChart.setOption(option);
            }).catch(error => 'An error occurred while loading the component');
        }

        showJson(){
            var test;
            if(window.XMLHttpRequest){
                test = new XMLHttpRequest();
            }else if(window.ActiveXObject){
                test = new window.ActiveXObject();
            }else{
                console.log("请升级至最新版本的浏览器");
            }
            if(test !=null){
                test.open("GET","./json.json",true);
                test.send(null);
                test.onreadystatechange=function(){
                    if(test.readyState==4&&test.status==200){
                        var obj = JSON.parse(test.responseText);
                        for (var name in obj){
                            console(obj[name].key);
                        }
                    }
                };

            }
        }

        componentDidMount() {

            this._init()
        }


        componentWillMount() {
            //this.showJson();
            console.log(this.state.optionECharts)
        }


        render() {

            const newProps = {
                name: "cqm",
                value: "testData",
            }

            return (
                <div>
                    <div id={'ec'} style={{width: 500, height: 500}}></div>
                    <WrappedComponent {...this.props} {...newProps}/>
                </div>
            )
        }
    }

    return NewComponent

}

export default Foundation;

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