1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00
next.js/test/integration/production/pages/dynamic/bundle.js
Arunoda Susiripala 9df59c1176 Dynamic component support with multiple modules (#2235)
* Layout ground works for next/async

* Implement the Dynamic Bundle feature.

* Add some test cases.

* Update README.

* Implement props aware dynamic bundle API.

* Update tests and README.

* Add a test case for React Context support.
2017-06-16 18:49:34 +05:30

69 lines
1.4 KiB
JavaScript

import React from 'react'
import dynamic from 'next/dynamic'
import Router from 'next/router'
import PropTypes from 'prop-types'
const HelloBundle = dynamic({
modules: (props) => {
const components = {
HelloContext: import('../../components/hello-context'),
Hello1: import('../../components/hello1')
}
if (props.showMore) {
components.Hello2 = import('../../components/hello2')
}
return components
},
render: (props, { HelloContext, Hello1, Hello2 }) => (
<div>
<h1>{props.title}</h1>
<HelloContext />
<Hello1 />
{Hello2? <Hello2 /> : null}
</div>
)
})
export default class Bundle extends React.Component {
static getInitialProps ({ query }) {
return { showMore: Boolean(query.showMore) }
}
static childContextTypes = {
data: PropTypes.object
}
getChildContext () {
return {
data: { title: 'ZEIT Rocks' }
}
}
toggleShowMore () {
if (this.props.showMore) {
Router.push('/dynamic/bundle')
return
}
Router.push('/dynamic/bundle?showMore=1')
}
render () {
const { showMore } = this.props
return (
<div>
<HelloBundle showMore={showMore} title="Dynamic Bundle"/>
<button
id="toggle-show-more"
onClick={() => this.toggleShowMore()}
>
Toggle Show More
</button>
</div>
)
}
}