Answers for "force a component to rerender"

1

React best way of forcing component to update

this.forceUpdate();
Posted by: Guest on June-11-2020
0

force a component to rerender

import React, { useState } from 'react';

//create your forceUpdate hook
function useForceUpdate(){
    const [value, setValue] = useState(0); // integer state
    return () => setValue(value => value + 1); // update the state to force render
}

function MyComponent() {
    // call your hook here
    const forceUpdate = useForceUpdate();
    
    return (
        <div>
            {/*Clicking on the button will force to re-render like force update does */}
            <button onClick={forceUpdate}>
                Click to re-render
            </button>
        </div>
    );
}
Posted by: Guest on December-29-2021

Code answers related to "force a component to rerender"

Code answers related to "Javascript"

Browse Popular Code Answers by Language