Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

🤿Alura Imersão React_ (NextJs) 3ª Edição.

NotificationsYou must be signed in to change notification settings

marcosbarker/alura.imersaoReact-Alurakut

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

This example features how you use a different styling solution thanstyled-jsx that also supports universal styles. That means we can serve the required styles for the first render within the HTML and then load the rest in the client. In this case we are usingstyled-components.

For this purpose we are extending the<Document /> and injecting the server side rendered styles into the<head>, and also adding thebabel-plugin-styled-components (which is required for server side rendering). Additionally we set up a globaltheme for styled-components using NextJS custom<App> component.

code:yarn create next-app --example with-styled-components

Preview

Preview the example live onStackBlitz:

Open in StackBlitz

Deploy your own

Deploy the example usingVercel:

Deploy with Vercel

How to use

Executecreate-next-app withnpm orYarn to bootstrap the example:

npx create-next-app --example with-styled-components with-styled-components-app# oryarn create next-app --example with-styled-components with-styled-components-app

Deploy it to the cloud withVercel (Documentation).

Try it on CodeSandbox

Open this example on CodeSandbox

Notes

When wrapping aLink fromnext/link within a styled-component, theas prop provided bystyled will collide with the Link'sas prop and cause styled-components to throw anInvalid tag error. To avoid this, you can either use the recommendedforwardedAs prop from styled-components or use a different named prop to pass to astyled Link.

Click to expand workaround example

components/StyledLink.js

importLinkfrom'next/link'importstyledfrom'styled-components'constStyledLink=({ as, children, className, href})=>(<Linkhref={href}as={as}passHref><aclassName={className}>{children}</a></Link>)exportdefaultstyled(StyledLink)`  color: #0075e0;  text-decoration: none;  transition: all 0.2s ease-in-out;  &:hover {    color: #40a9ff;  }  &:focus {    color: #40a9ff;    outline: none;    border: 0;  }`

pages/index.js

importStyledLinkfrom'../components/StyledLink'exportdefault()=>(<StyledLinkhref="/post/[pid]"forwardedAs="/post/abc">    First post</StyledLink>)

[8]ページ先頭

©2009-2025 Movatter.jp