Giter VIP home page Giter VIP logo

learn-next's Introduction

Next.js学习笔记

创建Layout组件的三种方法

  1. const Layout = props => (
      <div style={layoutStyle}>
        <Header />
        {props.children}
      </div>
    );
    
    const Index = () => (
      <Layout>
        <p>Hello Next.js</p>
      </Layout>
    );
  2. const withLayout = Page => {
      return () => (
        <div style={layoutStyle}>
          <Header />
          <Page />
        </div>
      );
    };
    
    const Page = () => <p>Hello Next.js</p>;
    
    export default withLayout(Page);
  3. const Layout = props => (
      <div style={layoutStyle}>
        <Header />
        {props.content}
      </div>
    );
    
    const indexPageContent = <p>Hello Next.js</p>;
    
    export default function Index() {
      return <Layout content={indexPageContent} />;
    }

路由

next框架封装了router和redux,可以直接使用

import { useRouter } from 'next/router';
import Link from 'next/link';
<Link href="/p/[id]" as={`/p/${props.id}`}>
  <a>{props.id}</a>
</Link>

Link组件href属性渲染文件的路径,as属性表示地址栏显示的URL。

动态路由

pages/p/[id].js,[]内是参数,在[id].js文件中通过router.query.id读取

请求数据

getInitialProps中请求数据,成功后可在props中使用

// 在React组件中使用
import React from 'react'

export default class extends React.Component {
  static async getInitialProps({ req }) {
    const userAgent = req ? req.headers['user-agent'] : navigator.userAgent
    return { userAgent }
  }

  render() {
    return (
      <div>
        Hello World {this.props.userAgent}
      </div>
    )
  }
}

// 在stateless组件中使用
Index.getInitialProps = async function(context) {
  const { id } = context.query;
  const res = await fetch('https://api.tvmaze.com/search/shows?q=batman');
  const data = await res.json();

  console.log(`Show data fetched. Count: ${data.length}`);

  return {
    shows: data.map(entry => entry.show)
  };
};

注:请求过程在服务端完成,所以浏览器不会有控制台输出和网络请求。当使用Link组件时,跳转在客户端完成,控制台输出和网络请求发生在浏览器中。

样式

Next.js推荐Css in Js

注意作用域,父组件样式不影响子组件

<style jsx global>
<style jsx>{`
    h1,
    a {
      font-family: 'Arial';
    }

    ul {
      padding: 0;
    }

    li {
      list-style: none;
      margin: 5px 0;
    }

    a {
      text-decoration: none;
      color: blue;
    }

    a:hover {
      opacity: 0.6;
    }
`}</style>

learn-next's People

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.