preface
React source code, I want to debug, and then the debugger to see how variables and data are transferred, but I have no choice. There’s so much stuff to pack. A lot of potholes. Keep a record. Help people tread less potholes, and make a note for yourself.
The preparatory work
-
Node v8.0.0+ and Yarn V1.2.0 +.
-
JDK has been installed.
-
You have GCC installed (or don’t bother if you need to install a compiler), because some dependencies may have to be compiled, and on OS X, the Xcode command-line tool takes care of that for you; On Ubuntu, apt-get install build-essential installs the required package (similar commands on other Linux distributions work as well); On Windows you need to take some extra steps, as shown in the Node-gyp installation steps.
-
Familiar with Git.
-
One can run the React project (create-react-app)
start
1. Obtain the source code
- Open source address: github.com/facebook/re…
- Clone to its own local
- You can download the zip and unzip it
- You can also Fork to GitHub and Clone
- My personal advice is to Fork, then modify, add comments, etc. Keep a record of your learning experience
- You can Clone it directly
2. Establish a dependency connection
- Open the project
- Run YARN to install dependencies
- run
yarn build react/index,react/jsx,react-dom/index,scheduler --type=NODE
repack cd build/node_modules/react
Enter the React packageyarn link
Establish a connectionCD to the original project path
Return to prepare for the next entry into the React – DOMcd build/node_modules/react-dom
yarn link
3. Find a React project that you can debug
I’m using create-react-app here
npx create-react-app my-app
cd my-app
4. Connect the project to the React library
- Of the project that was just created
node_modules
In thereact
andreact-dom
- Open the React project
yarn link react react-dom
5. Start debugging
-
Open up our Clone project
-
Find where you want to debug
- The change here is
packages/react-dom/src/client/ReactDOMLegacy.js
- The following
export function render( element: React$Element<any>, container: Container, callback: ? Function, ) {+ console.log(222); if (__DEV__) { console.error( 'ReactDOM.render is no longer supported in React 18. Use createRoot ' + 'instead. Until you switch to the new API, your app will behave as ' + "if it's running React 17. Learn " + 'more: https://reactjs.org/link/switch-to-createroot', ); } invariant( isValidContainerLegacy(container), 'Target container is not a DOM element.', ); if (__DEV__) { const isModernRoot = isContainerMarkedAsRoot(container) && container._reactRootContainer === undefined;if (isModernRoot) { console.error( 'You are calling ReactDOM.render() on a container that was previously ' + 'passed to ReactDOM.createRoot(). This is not supported. ' + 'Did you mean to call root.render(element)? ', a); } } return legacyRenderSubtreeIntoContainer( null, element, container, false, callback, ); }Copy the code
- The change here is
-
Open the React source project
-
Run the yarn build react/index, the react/JSX, react – dom/index, the scheduler – type = NODE repackaged
-
Open the React project we created
-
yarn start
-
Open the console
6. Subsequent
This is required every time you modify the source code
- run
yarn build react/index,react/jsx,react-dom/index,scheduler --type=NODE
repack - Refresh our page
- It’s really a hassle and there are no good ideas at the moment
- Maybe this is practice!
conclusion
Feel free to leave a comment below if you have any questions. Give me a thumbs up