Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introducing Now and Next.js

Introducing Now and Next.js

now と next.js の紹介

Naoyuki Kanezawa

November 13, 2016
Tweet

More Decks by Naoyuki Kanezawa

Other Decks in Programming

Transcript

  1. Get started 1. next.js ΛΠϯετʔϧ 2. next ίϚϯυΛ࣮ߦ 3. pages

    σΟϨΫτϦʹ Reactίϯϙω ϯτΛฦ͢JSϑΝΠϧΛ࡞੒
  2. import React from 'react' import css from 'next/css' const style

    = css({ background: ‘red’, ':hover': { background: 'gray' }, '@media (max-width: 600px)': { background: 'blue' } }) export default () => <p className={style}>hi!</p>
  3. import React from 'react' import Link from 'next/link' export default

    () => ( <div><Link href=“/about”>͜͜</Link>ΛΫϦοΫ</div> )
  4. import React from 'react' import Head from 'next/head' export default

    () => ( <div> <Head> <title>λΠτϧ</title> <meta charSet="utf-8" /> </Head> <p>hi</p> </div> )
  5. IsomorphicʹσʔλΛऔಘ͢Δྫ import React from ‘react' import 'isomorphic-fetch' export default class

    extends React.Component { static async getInitialProps () { // CORS͕༗ޮͳAPIʹαʔόɺΫϥΠΞϯτ૒ํ͔ΒΞΫηε const res = await fetch(‘http://api.example.com/user/123’) const user = await res.json() return { user } } render () { return <p>hi, {this.props.user.name}!</p> } }
  6. now

  7. ϙʔτΛҰͭ଴ͪड͚Δ const http = require(‘http’) http.createServer((req, res) => { res.writeHead(200,

    { 'Content-Type': ‘text/plain' }) res.end(‘Hello, world’) }).listen(3000, () => { console.log(‘Ready!’) })
  8. microΛ࢖ͬͨαʔό࣮૷ const { json } = require(‘micro’) module.exports = async

    (req, res) => { // ϦΫΤετϘσΟͷऔಘ const body = await json(req) console.log(body.data) // Ϩεϙϯε return { message: ‘ok’ } }