-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
callback for setAttributes? #5596
Comments
Could you describe such an instance? To have a better understanding of the use-case and whether callback is the most appropriate solution. |
In my case I am creating a block that formats code, and I need to retain my cursor position after tabbing. Below is the only way I could get it to work that I could think of, since the position needed to be placed after the attributes were set:
I would be interested to know if there are other strategies people have used for something like this. I imagine there may be situations for an API call as a callback? You may want to set the state and then call out using the state only after state has updated. |
At this time, we'll not support a callback for For your use-case, I'd suggest one of:
|
@aduth Since setAttributes is very similar to setState, this would follow the standard set by React itself. Can I ask why this is not being considered? |
Despite the similar name, There was a discussion not too long ago about this in Slack, in case it helps provide additional context (link requires registration): https://wordpress.slack.com/archives/C02QB2JS7/p1545162410122400 |
@aduth Thanks for responding. Since I posted this I've been pouring through Gutenberg to try and understand why this can't be a thing. I realized that underneath the actions are called by Redux, and that Redux doesn't have a callback like that. Rather it appears it would require something like Anyway, for the time being I'm just using setTimeout and setInterval to check the value of attributes as mentioned by the OP. |
I'd recommend considering A contrived example: blockSettings.edit = class extends wp.element.Component {
constructor() {
super( ...arguments );
this.setIsClicked = this.setIsClicked.bind( this );
}
componentDidUpdate( prevProps ) {
if ( this.props.attributes.isClicked && ! prevProps.attributes.isClicked ) {
// After attribute changed.
}
}
setIsClicked() {
this.props.setAttributes( {
isClicked: true,
} );
}
render() {
return <button type="button" onClick={ this.setIsClicked }>Click</button>
}
}; See: https://reactjs.org/docs/react-component.html#componentdidupdate |
I was hoping to avoid needing to do a ton of prop drilling. If I rely on componentDidUpdate as opposed to a callback function, I have to prop drill every variable from the parent to my child components. |
Thanks for posting your workaround for us @JimSchofield I just had to do this because I am using a ServerSideRender output for a block. It uses a custom JS to load the data in an iframe. We have it set up to use a preview button, but when the preview is active and the attributes change it doesn't update the preview because the JS needs to be reloaded. So I have set a time to do a refresh. Seems to work for now, but it would have been much nicer if there was a way to just call the function after |
Issue Overview
There are instances where a callback for setAttributes might make sense
Current Issue
Current Workaround
I got it to work sufficiently by doing a setTimeout, but I am not sure why or how reliably it would work:
Possible Solution
Might we include a callback like in React's setState method?
Related Issues and/or PRs
I apologize if this has been discussed before, but I couldn't find an instance where this was suggested.
I also am aware this may be solved in other ways, but it seems like including some parallel capability to setState would make sense here if possible.
The text was updated successfully, but these errors were encountered: