zoukankan      html  css  js  c++  java
  • react高阶函数组件

    Layout as a Higher Order Component

    // components/MyLayout.js
    
    import Header from './Header';
    
    const layoutStyle = {
      margin: 20,
      padding: 20,
      border: '1px solid #DDD'
    };
    
    const withLayout = Page => {
      return () => (
        <div style={layoutStyle}>
          <Header />
          <Page />
        </div>
      );
    };
    
    export default withLayout;
    
    // pages/index.js
    
    import withLayout from '../components/MyLayout';
    
    const Page = () => <p>Hello Next.js</p>;
    
    export default withLayout(Page);
    
    // pages/about.js
    
    import withLayout from '../components/MyLayout';
    
    const Page = () => <p>This is the about page</p>;
    
    export default withLayout(Page);
    

    Method 2 - Page content as a prop

    // components/MyLayout.js
    
    import Header from './Header';
    
    const layoutStyle = {
      margin: 20,
      padding: 20,
      border: '1px solid #DDD'
    };
    
    const Layout = props => (
      <div style={layoutStyle}>
        <Header />
        {props.content}
      </div>
    );
    
    export default Layout;
    
    // pages/index.js
    
    import Layout from '../components/MyLayout.js';
    
    const indexPageContent = <p>Hello Next.js</p>;
    
    export default function Index() {
      return <Layout content={indexPageContent} />;
    }
    
    // pages/about.js
    
    import Layout from '../components/MyLayout.js';
    
    const aboutPageContent = <p>This is the about page</p>;
    
    export default function About() {
      return <Layout content={aboutPageContent} />;
    }
    

    参考:https://nextjs.org/learn/basics/using-shared-components/rendering-children-components

  • 相关阅读:
    【NOIP2003提高组】加分二叉树
    【luogu1220】关路灯
    【luogu2583】地铁间谍
    Hello, World!
    python中的画笔控制函数
    python中库引用与import
    python中RGB色彩
    turtle角度坐标体系
    turtle空间坐标系
    python中turtle库的使用
  • 原文地址:https://www.cnblogs.com/joe-yang/p/11365427.html
Copyright © 2011-2022 走看看