Improve documentation on browser integration with React
See original GitHub issueThe 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:
- Created 3 years ago
- Reactions:4
- Comments:9 (6 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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!
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!