admin管理员组文章数量:1335847
I am attempting to put together a custom block that produces an embed from a third party. The embed is generated by an external script scanning for an element with a data attribute holding a unique identifier. I have the basic version of this block working just fine. However, I am now trying to have this embed display dynamically in the edit screen when you enter the identifier.
My edit function looks like this:
function Edit( { attributes, setAttributes } ) {
const onChangeIdentifier = (value) => {
setAttributes({ identifier: value });
};
return (
<div id = {`embed-${ instanceId }`}>
{attributes.identifier
?
<div
data-identifier= { attributes.identifier }
>
</div>
:
<Placeholder
instructions={ __( 'Enter identifier.' ) }
>
<PlainText
placeholder={ __(
'Enter identifier here…'
) }
value={ attributes.identifier || '' }
onChange={ onChangeIdentifier }
/>
</Placeholder>
}
</div>);
}
export default withInstanceId( Edit );
Almost everything happens correctly. When the identifier is entered it swaps out the placeholder for the data-identifier element, but the embed can't be generated because the third party script only automatically triggers a scan when the DOM loads. They do provide a function to manually call a scan in the event you create an embed via ajax. Here is the issue: I do not know where to call this function to have it correctly render the embed. (I can verify it works properly, as I can edit the block and call the function in console and have it update correctly.)
I am attempting to put together a custom block that produces an embed from a third party. The embed is generated by an external script scanning for an element with a data attribute holding a unique identifier. I have the basic version of this block working just fine. However, I am now trying to have this embed display dynamically in the edit screen when you enter the identifier.
My edit function looks like this:
function Edit( { attributes, setAttributes } ) {
const onChangeIdentifier = (value) => {
setAttributes({ identifier: value });
};
return (
<div id = {`embed-${ instanceId }`}>
{attributes.identifier
?
<div
data-identifier= { attributes.identifier }
>
</div>
:
<Placeholder
instructions={ __( 'Enter identifier.' ) }
>
<PlainText
placeholder={ __(
'Enter identifier here…'
) }
value={ attributes.identifier || '' }
onChange={ onChangeIdentifier }
/>
</Placeholder>
}
</div>);
}
export default withInstanceId( Edit );
Almost everything happens correctly. When the identifier is entered it swaps out the placeholder for the data-identifier element, but the embed can't be generated because the third party script only automatically triggers a scan when the DOM loads. They do provide a function to manually call a scan in the event you create an embed via ajax. Here is the issue: I do not know where to call this function to have it correctly render the embed. (I can verify it works properly, as I can edit the block and call the function in console and have it update correctly.)
Share Improve this question asked May 27, 2020 at 22:48 jshwlkrjshwlkr 5341 gold badge6 silver badges24 bronze badges1 Answer
Reset to default 1You might try a useEffect
hook subscribed to the attributes.identifier
to call the script.
something like:
function Edit( { attributes, setAttributes } ) {
useEffect( () => {
// this will fire whenever the attribute changes.
thirdPartyScriptFunctionName();
},[attributes.identifier])
const onChangeIdentifier = (value) => {
setAttributes({ identifier: value });
};
return (
<div id = {`embed-${ instanceId }`}>
{attributes.identifier
?
<div
data-identifier= { attributes.identifier }
>
</div>
:
<Placeholder
instructions={ __( 'Enter identifier.' ) }
>
<PlainText
placeholder={ __(
'Enter identifier here…'
) }
value={ attributes.identifier || '' }
onChange={ onChangeIdentifier }
/>
</Placeholder>
}
</div>);
}
export default withInstanceId( Edit );
You'll need to get the useEffect
hook from the wp.element global.
Hope this helps!
本文标签: javascripteventcallback on block update
版权声明:本文标题:javascript - eventcallback on block update? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742389340a2465676.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论