Advertisement
Guest User

Untitled

a guest
Sep 20th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. # Next.JS Notes
  2. - Server rendered React
  3. - Pages in pages dir correspond to routes in app
  4. - Don't need to bring in React for every page
  5. - Hot code reloading out of box for Next
  6.  
  7. ## Install
  8. `npm i next react react-dom`
  9. - Add dev script in package.json: `dev`: `next`
  10. - *if diff port than 3000 needed*: `dev`: `next -p <port>`
  11. - Create `pages` dir at root
  12. - Run `dev`, .next folder is generated
  13.  
  14. ## Init Pages
  15. - Add index.js to pages dir:
  16. ```react
  17. const Index = () => (
  18. <div>Index Page</div>
  19. )
  20.  
  21. export default Index
  22. ```
  23.  
  24. ## Adding Layout:
  25. - Create `components/Layout.js`
  26. ```react
  27. export default ({ children, title }) => (
  28. <div>
  29. <header>
  30. <Link href="/"><a>Home</a></Link>
  31. <Link href="/new"><a>New</a></Link>
  32. </header>
  33. <h1>{title}</h1>
  34. {children}
  35. <footer>footer</footer>
  36. </div>
  37. )
  38. ```
  39. - Use Layout: <Layout title="stuff">{everything-else}</Layout>
  40.  
  41. ## Routing: Use History API, not server requests for new routes
  42. - Add `import Link from "next/link"`
  43. ```react
  44. <Link href="/"><a>Go to page</a></Link>
  45. ```
  46.  
  47. ## Serving Images
  48. - Add `static` folder at root, add imgs
  49. - Reference img in code simply with: `<img src="/static/<img>.png">`
  50.  
  51. ## Add styling
  52. ```react
  53. <style jsx>{`
  54. .root {
  55. display: flex;
  56. justify-content: center;
  57. flex-direction: column;
  58. }
  59.  
  60. `}
  61. </style>
  62. ```
  63.  
  64. ## Add global styling
  65. ```react
  66. <style global jsx>(`
  67. body {
  68. margin: 0;
  69. font-size: 110%;
  70. }
  71. `)
  72. </style>
  73. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement