Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I edit React components without reloading the browser?

If React offers DOM reconciliation, is it possible to dynamically reload component's code and re-render it after I edit it?

I'm looking for a solution that allows me to edit JSX file, save it, and have the component update itself in the browser, without reloading the page, unmounting it or losing its state.

Ideally this should work without browser plugins.

like image 459
Dan Abramov Avatar asked Jul 24 '14 22:07

Dan Abramov


People also ask

How can I change URL in React without reloading?

When you're inside a react component, the props has history , you can use that. Then you can import browserRouterRef anywhere and use browserRouterRef. current. history to change the route even if you are not inside a react component.

How do you refresh a page in React without reloading?

reload(false); This method takes an optional parameter which by default is set to false. If set to true, the browser will do a complete page refresh from the server and not from the cached version of the page.

How do you update a component without refreshing full page react native?

You can do something like this. In the Quote. js create getNewQuote function and call that function inside the useEffect . Then, pass the getNewQuote function to the Button component and on New Quote button onClick set getNewQuote .


1 Answers

You can use react-hot-loader, a drop-in Webpack loader that enables live editing for React components in your projects. No browser plugins or IDE hooks required.

It marries Webpack Hot Module Replacement (HMR) with React.

You can use this if:

  • Your React components donʼt have nasty side-effects;
  • Youʼre willing to switch to Webpack for modules (it's not hard to switch, see the walkthrough);
  • You have a spare couple of hours (minutes if you already use Webpack).

How it works:

  • It uses Webpack HMR API to learn about the “module update available” event.
  • It changes React.createClass calls to special createClass and updateClass functions that store the component's prototype and later update it with fresh version;
  • When all prototypes are updated, it calls forceUpdate to re-render the components.

There is a demo video, an explanatory blog post and a React tutorial app fork with live-edit configured.

And it's all vanilla JS.

like image 155
Dan Abramov Avatar answered Oct 28 '22 06:10

Dan Abramov