• This is a sample block I’m working on, it’s intended to use a simple form, not to work with React. The problem is with the block events Focus and Blur, when you click outside the block, the content is re-rendered and when it does that, the form loses the input value because there is no attribute assigned to this field to re-input the value. How can I prevent this re-render action?

    I’ve tried to use shouldComponentUpdate() but it’s not calling this function.

    (function(wp, $) {
    	
    	var blocks = wp.blocks;
    	var el = wp.element.createElement;
    	var withState = wp.compose.withState;
    		
    	blocks.registerBlockType('plugin/test', {
    		
    		title: 'Plugin Test',
    		icon: 'universal-access-alt',
    		category: 'layout',
    	
    		supports: {
    			html: false,
    		},
    
    		// This is not working!!
    		shouldComponentUpdate(nextProps, nextState) {
    			console.log(nextProps, nextState);
    			return this.state.editing ? false : true;
    		},
    					
    		edit: withState({
    			editing: false,
    		})(function(props) {
    			
    			return el('div', {
    				ref: function(container) {
    
    					// How to stop re-render this and loses input value?
    					$(container).html('<form><input type="text"></form>');
    
    				}
    			});
    			
    		}),
    	
    	    save: function() {
    	        return null
    	    }
    	    
    	});
    	
    })(window.wp, jQuery);
Viewing 4 replies - 1 through 4 (of 4 total)
  • Hi @edir

    Thanks for getting in touch. I have a few follow up questions.

    The problem is with the block events Focus and Blur

    What specific block type are you working with?

    It’d be worth taking a look through Github to see if any one else has reported a similar question: https://github.com/wordpress/gutenberg/

    Thread Starter Edir Pedro

    (@edir)

    I don’t know if this could be an issue or not because I don’t know React, but I posted on github too.

    https://github.com/WordPress/gutenberg/issues/8800

    I’m working to replace my old page builder and I use the Advanced Custom Field plugin to build block sections like Gutenberg does, but using simple forms with the ACF. I can’t reshape all my tools to use Gutenberg, so I decided to build a small plugin to make an ACF form works inside the Gutenberg editor. That’s why I need to render a form inside the block and why the form can’t interact with React JS. It’s not the best solution, but it’s working.

    I found this call shouldComponentUpdate on React documentation, but it’s not working with the block component.

    Thread Starter Edir Pedro

    (@edir)

    @lizkarkoski

    I’ll use a simple form inside the block and when you change the focus, clicking outside the block or clicking in the block to select it, it renders the form again and this can’t be done in my case.

    Thread Starter Edir Pedro

    (@edir)

    I found a better way to make this work, instead of open the form inside Gutenberg, i choose to open inside a modal, out of the editor. After submit the form i just force the block to render again and get the changes by using a server side render.

    But the only problem now is: How to make the block to update?

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How to stop re-render the block when it loses focus?’ is closed to new replies.