mirror of
https://github.com/terribleplan/next.js.git
synced 2024-01-19 02:48:18 +00:00
Example/add high order component example (#2331)
* temporary commit * update code * completed example higher order component * remove custom server
This commit is contained in:
parent
e3f0fdb9db
commit
3c71e818bf
31
examples/with-higher-order-component/README.md
Normal file
31
examples/with-higher-order-component/README.md
Normal file
|
@ -0,0 +1,31 @@
|
|||
[![Deploy to now](https://deploy.now.sh/static/button.svg)](https://deploy.now.sh/?repo=https://github.com/zeit/next.js/tree/master/examples/with-higher-order-component)
|
||||
|
||||
# Higher Order Component example
|
||||
|
||||
## How to use
|
||||
|
||||
Download the example [or clone the repo](https://github.com/zeit/next.js):
|
||||
|
||||
Install it and run:
|
||||
|
||||
**npm**
|
||||
```bash
|
||||
npm install
|
||||
npm run dev
|
||||
```
|
||||
|
||||
**yarn**
|
||||
```bash
|
||||
npm install
|
||||
npm run dev
|
||||
```
|
||||
|
||||
Deploy it to the cloud with [now](https://zeit.co/now) ([download](https://zeit.co/download))
|
||||
|
||||
```bash
|
||||
now
|
||||
```
|
||||
|
||||
## About example
|
||||
|
||||
This example show you how to create Higher Order Component in next.js
|
26
examples/with-higher-order-component/components/withApp.js
Normal file
26
examples/with-higher-order-component/components/withApp.js
Normal file
|
@ -0,0 +1,26 @@
|
|||
import React from 'react'
|
||||
|
||||
function withApp (Child) {
|
||||
return class WrappedComponent extends React.Component {
|
||||
static getInitialProps (context) {
|
||||
return Child.getInitialProps(context)
|
||||
}
|
||||
render () {
|
||||
return (
|
||||
<div>
|
||||
<header>
|
||||
<h1>Header</h1>
|
||||
</header>
|
||||
<main>
|
||||
<Child greeting='Hello From HOC' {...this.props} />
|
||||
</main>
|
||||
<footer>
|
||||
<h1>Footer</h1>
|
||||
</footer>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default withApp
|
16
examples/with-higher-order-component/package.json
Normal file
16
examples/with-higher-order-component/package.json
Normal file
|
@ -0,0 +1,16 @@
|
|||
{
|
||||
"name": "with-higher-order-component",
|
||||
"version": "1.0.0",
|
||||
"scripts": {
|
||||
"dev": "next",
|
||||
"build": "next build",
|
||||
"start": "next start"
|
||||
},
|
||||
"dependencies": {
|
||||
"next": "latest",
|
||||
"react": "^15.4.2",
|
||||
"react-dom": "^15.4.2"
|
||||
},
|
||||
"author": "",
|
||||
"license": "ISC"
|
||||
}
|
20
examples/with-higher-order-component/pages/index.js
Normal file
20
examples/with-higher-order-component/pages/index.js
Normal file
|
@ -0,0 +1,20 @@
|
|||
import React from 'react'
|
||||
import withApp from '../components/withApp'
|
||||
|
||||
class Index extends React.Component {
|
||||
static getInitialProps (context) {
|
||||
const { isServer } = context
|
||||
return { isServer }
|
||||
}
|
||||
render () {
|
||||
const { greeting } = this.props
|
||||
return (
|
||||
<div>
|
||||
<h2>Index page</h2>
|
||||
<h3 style={{ color: 'red' }}>{greeting}</h3>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
export default withApp(Index)
|
Loading…
Reference in a new issue