react next 构建大屏实践

搭建基础布局

  • 参考资料: 地址
  • 使用typescript next9.0.0+内置typescript 地址
  • 目录结构

components

Header

index.less
index.tsx

Layout

index.less
index.tsx

pages

_app.tsx
index.tsx

  • 具体代码实现
  • Header/index.tsx
import React from "react";
import "./index.less";

const Header = ({ title }) => {
  return <div>{title}头部</div>;
};

export default Header;
  • Layout/index.tsx
import React from "react";
import Header from "../Header/index";

const Layout = ({ children, title }) => {
  return (
    <>
      {/* 头部 */}
      <Header title={title} />
      {children}
      底部
    </>
  );
};

export default Layout;
  • pages/_app.tsx
import App, { Container } from "next/app";
import React from "react";
import Layout from "../components/Layout/index";

class MyApp extends App {
  static async getInitialProps({ Component, ctx }) {
    let pageProps = {};
    if (Component.getInitialProps) {
      pageProps = await Component.getInitialProps(ctx);
    }
    return { pageProps };
  }

  // 在 Layout 传入参数 title,使每个页面可以设置不同的title。
  render() {
    // @ts-ignore
    const { Component, pageProps } = this.props;
    return (
      <Container>
        <Layout title={pageProps.title}>
          <Component {...pageProps} />
        </Layout>
      </Container>
    );
  }
}

export default MyApp;

报错

  • 使用bizCharts报错document is not defined
  • 解决:
import React from 'react';
import ReactDOM from 'react-dom';
let bizcharts;
if (process.browser) {
  bizcharts = require('bizcharts');
}

const data = [
  { genre: 'Sports', sold: 275, income: 2300 },
  { genre: 'Strategy', sold: 115, income: 667 },
  { genre: 'Action', sold: 120, income: 982 },
  { genre: 'Shooter', sold: 350, income: 5271 },
  { genre: 'Other', sold: 150, income: 3710 }
];

const cols = {
  sold: { alias: 'amount' },
  genre: { alias: 'game type' }
};

export default () => {
  return (
    <div>
      {process.browser &&
        <bizcharts.Chart width={600} height={400} data={data} scale={cols}>
          <bizcharts.Axis name="genre" />
          <bizcharts.Axis name="sold" />
          <bizcharts.Legend position="top" dy={-20} />
          <bizcharts.Tooltip />
          <bizcharts.Geom type="interval" position="genre*sold" color="genre" />
        </bizcharts.Chart>
      }
    </div>
  );
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值