鍍金池/ 問(wèn)答/HTML/ 高階組件返回的Class無(wú)法渲染出來(lái)。

高階組件返回的Class無(wú)法渲染出來(lái)。

使用高階組件返回class包裝后的組件無(wú)法正確渲染,但是直接將組件return可以渲染出來(lái),找不出問(wèn)題,請(qǐng)指教一下。多謝大家

function HOC(WrappedComponent){
    // 渲染不出來(lái)
    return class HOC extends Component {
        render(){
            const newProps = {type:'HOC'};
            return <WrappedComponent {...this.props} {...newProps}/>
        }
    }
    // 以下可以
    // return function newRender(props){
    //     return <WrappedComponent />
    // }
}

class OriginComponent extends Component {
    render(){
        return <div>這是原始組件</div>
    }
}

const newComponent = HOC(OriginComponent)

class TestHOCCom extends Component{
    constructor(){
        super()
        this.state = {
        }
    }
    render() {
        // console.log(newComponent, 9999999)
        return (
            <div>
                <p>頂層容器</p>
                <newComponent />
            </div>
        )
    }
}
export default TestHOCCom
回答
編輯回答
撥弦
const NewComponent = HOC(OriginComponent)
<NewComponent />

一定要用大寫(xiě)的首字母, 要不 jsx 會(huì)把它編譯成字符串標(biāo)簽.

2018年4月21日 16:02