question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Improve documentation on browser integration with React

See original GitHub issue

The getting started guide at https://mswjs.io/docs/getting-started/integrate/browser (and various other places) suggests the following to integrate with a React app.

// src/index.js
import React from 'react'
import ReactDOM from 'react-dom'
import App from './App'

if (process.env.NODE_ENV === 'development') {
  const { worker } = require('./mocks/browser')
  worker.start()
}

ReactDOM.render(<App />, document.getElementById('root'))

I found that with a very basic React app based on create-react-app that the app had rendered and made API calls before the service worker had finished starting and so it was unable to intercept them.

After a fair amount of frustration trying to figure out the problem, I tried the following and it resolved the issue. Can I suggest that the getting started guide and other documentation is updated to include something similar?

// src/index.js
import React from 'react'
import ReactDOM from 'react-dom'
import App from './App'

function renderApp() {
  ReactDOM.render(<App />, document.getElementById('root'))
}

if (process.env.NODE_ENV === 'development') {
  const { worker } = require('./mocks/browser')
  worker.start().then(() => renderApp())
} else {
  renderApp()
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:4
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
kudcommented, Aug 25, 2020

You saved my day @mike-trout

I was struggling with this, I understood that I had a lag between the rendering and the worker but didn’t understand exactly why my first requests weren’t caught by msw.

I thought it was the idea of https://mswjs.io/docs/api/setup-worker/start#waituntilready but apparently no, or it doesn’t work well…?

Well, thank you!

1reaction
kettanaitocommented, Sep 21, 2020

Documentation now has the “Deferred mounting” recipe.

This should be helpful as a reference to anyone who stumbles upon a similar issue. Thank you for raising this!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Optimizing Performance - React
If you're benchmarking or experiencing performance problems in your React apps, make sure you're testing with the minified production build. By default, React...
Read more >
Boost Productivity w/ a React Chrome Extension from the Top 13
Here are the top 13 React Chrome extensions and boost your developer productivity. All tools are on React Chrome extension marketplace.
Read more >
A guide to features and updates in React DevTools
The React DevTools browser extension for debugging web-based React applications is available on some popular web browsers. You can install it ...
Read more >
Create chrome extension with ReactJs using inject page ...
Chrome is an awesome browser from Google that is very fast and lightweight , yet also very powerful. Chrome also has a very...
Read more >
Getting started with React - Learn web development | MDN
Familiarity with both HTML and JavaScript will help you to learn JSX, and better identify whether bugs in your application are related to ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found