Skip to content

Instantly share code, notes, and snippets.

@iamshubhamjangle
Last active January 21, 2024 15:17
Show Gist options
  • Save iamshubhamjangle/fb71e22ee6798f23f9a7b07651165dc7 to your computer and use it in GitHub Desktop.
Save iamshubhamjangle/fb71e22ee6798f23f9a7b07651165dc7 to your computer and use it in GitHub Desktop.
Next.js Tips & Tricks

Setting up new yarn nextjs project

yarn create next-app my-app     # Create new project my-app
yarn install                    # Install all dependencies
yarn dev                        # Run development server
yarn add lodash                 # Add new package
yarn remove lodash              # Remove a package

Depends on what version your on. For 12 next/future/image and 13 here's a fun trick:

<Image
  alt={altText}
  src={url}
  height={imageHeight}
  width={imgeWidth}
  style={{ height: 'auto', objectFit: 'contain', position: 'relative' }}
/>

This will keep the image inside of it's parent element and keep it responsive. No need to do anything else with the parent. But to keep images from getting to big and looking OK on the page, I do something like this:

<div style={{ margin: 'auto', maxWidth: '500px' }}>
  <Image
    alt={altText}
    src={url}
    height={imageHeight}
    width={imgeWidth}
    style={{ height: 'auto', objectFit: 'contain', position: 'relative' }}
  />
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment