DEV Community

Kyle Hoskins
Kyle Hoskins

Posted on

๐Ÿ™… Never copy/paste when youโ€™re learning React (or any other library/language)

Learning a new language or library and thinking about copying and pasting that Stack Overflow chunk of code, tutorial example, or "Getting Started" code block?

Think again!

Why not? Let's examine...

The Benefits of "typing it out"

Taking the extra time to type something out rather than copying and pasting it gives you several advantages:

๐Ÿง  Memory

The learning period is the most important phase to take the extra time to ensure you're getting in actual practice.

Take a breath, take it slow, and you will be thankful that you actually remember the syntax later. (... and don't worry, your code typing speed will become much faster! ๐Ÿš€)

๐Ÿง‘โ€๐Ÿ’ป Interviews

Ctrl+C, Ctrl+P can be a good tool for speed in some cases, but it won't help much when you're expected to quickly code for yourself in a fast-paced, high pressure situation.

Would you copy/paste or type-out the following basics?

npm install --save-dev @testing-library/react
// It's beneficial to know terminal basics 
// like npm, git, etc
Enter fullscreen mode Exit fullscreen mode
test('loads and displays greeting', () => {
  render(<Fetch url="/greeting" />)
  expect(screen.getByText('Load Greeting')).toBeInTheDocument()
})
// Without looking, can you write a complete test spec 
// file from scratch?
Enter fullscreen mode Exit fullscreen mode
import React, { useState } from 'react';
import IconButton from '@mui/material/IconButton';
import SplitRow from 'components/layout/SplitRow';
// Manually typing imports helps understand and remember
// libraries and project structure
Enter fullscreen mode Exit fullscreen mode

โ™ป๏ธ Refactoring/Reuse

Sometimes the ease of copy/paste prevents us from realizing what should be refactored into its own method or component.

You'll quickly become annoyed by typing repetitive code and start enjoying creating re-usable blocks as you code rather than coming back to it later.

โœ… Code Quality

When you're writing a new file/code block, it's tempting to copy/paste similar code from somewhere else in your project and make small adjustments.

However, hastily copy/paste/adjusting may leave you with a "hack" or other issues like sub-optimal naming conventions. Additionally, you may have already learned a better way to accomplish the task than in your previous code.

I hope you'll enjoy copying/pasting less and coding more!

P.S. Sometime's screen real estate is an issue, so I use a learning hack of copying and pasting into my file, typing out the same code above the pasted block, then deleting the pasted code

Top comments (0)