Metamask: How to reload only one component rather then whole page when active chain is changed?

Digs Population Dinamic Company in Answer on Metamask

In the absence of some, some of the policies found the general problem for the use of the dissolution of the Metamask in its web -the advantages. This can be discharged for the delicacies, which are pushed to the production and minimizes loads on the pages.

In this state, we disperse the simple reactions, which allows you to re -form only one component of the change of actual steady.

Problem:

Metamask: How to reload only one component rather then whole page when active chain is changed?

When Metamask goes out “warm”, it launches a full rendering. This can be a crook for the polls, which are already connected with the honeycomb connection or by the greatest records in their list of management.

Resolution:

To solve this problem, we make a personal reaction brush, which is extinguished, when the activist is changed and only overpowers the component. That’s the code:

`JSX

Import {USEFECT, USESTE} from 'react';

Const USERloadcomponent = (ComponentKey) => {

Const [ActiveChain, SetactiveChain] = Usestate (Window.ethereumchains [0]);

Using Effect (() => {

IF (Window.ethereum) {

Window.ethereum.on ('Chainhanged', () => {

SetactiveChain (Window.ethereumchains.find ((Tep) => catein.id == ActiveCain));

});

}

}, [ActiveChain]);

Return <> {/ Componer to re -tailor /} ;

};

Export Uselodcomponent Prefefinito;

Enlightenment:

In the predicted edge of the code:

  • We delay the personalized kryuk reactionsUselodcomponentnta ‘, which takes the add -ant parameter” Componentkey “.

  • Krik uses “Usestate” for the archivocation of the adaptive steady and its idyntifikator in the expanded local state (correspondingly “ActiveChainid”).

  • In the “Using” we check out, sublime metamas. If this is yes, we listen to “Think on Window”.

  • When the activist is changed (then there is a change of Awilacchainid), we extend the state” ActiveChain “, using the most obsessive identification

  • We unravel the JSX element, which will report to the change of activation, content of the component.

Using Prime:

`JSX

Import reaggerates from "react";

Import UserloadComponent from './usereloadcomponent';

Const component = () => {

// Select here a little counter ...

};

Const componentb = () => {

// Select here the second counterpart ...

};

FUNCTION App () {

Const Componentkey = 'Component-A'; // Choose a stingy component

Const {ActiveChain, SetactiveChain} = userloadcomponent (componentkey);

Returned (

Active Teap: {ActiveChain}

setactiveChain (Window.eThereumchains.find ((Tep) => chains.id == ActiveChain))}> Switty to {ActiveChain}

);

}

In this one, we redeem two components (component ‘componentb), which will be restored to the change of activated steeper. We are overwhelmed by the "Componentkey" overwhelming "componentkey"Uselodcomponent, which allows us to choose a stingy component.

Conclusion:

Userloadcomponento, you can be used by the use of a single component, when the actual chapter changes without a full -scale guttering. This resolution should be easily integrated into the reliance of react and make up the best polish experience for tech, which is the copper sets or more in their list.

liquidity staking pool