簡體   English   中英

componentDidMount 沒有被調用

[英]componentDidMount not getting called

在編輯和保存中古騰堡塊中使用的簡單組件:在這個簡化的示例中,它只是在 componentDidMount 中設置一個 state 變量,然后讓 render() 將其拾取並顯示。 這在后端按預期工作,但是當我嘗試查看頁面 render() 時看不到變量,它不存在。 解決問題變得更加困難,因為 console.logs 沒有顯示在前端的控制台中。 我不知道這是否是同一問題的一部分。 想法贊賞。

( function( blocks, element ) {

    blocks.registerBlockType( 'daisy/grid-view', {
        title: 'Daisy: Single Image View',
        icon: 'universal-access-alt',
        category: 'common',

        attributes: {
            pid         : {type: 'number'},
        },

        edit: function( props ) {

        return( 
        <p>
            <DaTest></DaTest>
        </p>)

        },
        save: function( props ) {

            console.log( "save")
            return (

                <p>xxx
                    <DaTest></DaTest>
                </p>
            )
        },
    } );
}(
    window.wp.blocks,
    window.wp.element,
    window.wp.editor

) );

--

class DaTest extends React.Component {

    render() {
        console.log( "in test")

        var xxx = JSON.stringify( this.state )
        return  (
            <div>
                inside DaTest Render [{xxx }] // xxx shows as {}
            </div>
        )
    }
    componentDidMount() {
        this.setState( {aaa : "aaa" })
    }
}

所以看起來我誤解了古騰堡塊的工作方式。 在保存中傳遞的是渲染的 output,前端永遠看不到組件。

重新思考。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM