Skip to content

Commit

Permalink
refactor: make each component an AppContainer (#830)
Browse files Browse the repository at this point in the history
Fix #806
  • Loading branch information
theKashey authored and gregberge committed Feb 4, 2018
1 parent 624866d commit 4e8e617
Show file tree
Hide file tree
Showing 23 changed files with 5,114 additions and 30 deletions.
11 changes: 11 additions & 0 deletions examples/async-portals/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"presets": [
"env",
"react"
],
"plugins": [
"react-hot-loader/babel",
"transform-class-properties",
"dynamic-import-node"
]
}
1 change: 1 addition & 0 deletions examples/async-portals/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
24 changes: 24 additions & 0 deletions examples/async-portals/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "hot-async-portals",
"version": "1.0.0",
"license": "MIT",
"scripts": {
"start": "webpack-dev-server --hot"
},
"devDependencies": {
"babel-core": "^6.26.0",
"babel-loader": "^7.1.2",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-preset-env": "^1.6.1",
"babel-preset-react": "^6.24.1",
"html-webpack-plugin": "^2.30.1",
"webpack": "^3.10.0",
"webpack-dev-server": "^2.9.7"
},
"dependencies": {
"react": "^16.2.0",
"react-dom": "^16.2.0",
"react-hot-loader": "next",
"react-portal": "^4.1.2"
}
}
21 changes: 21 additions & 0 deletions examples/async-portals/src/App.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { hot, setConfig } from 'react-hot-loader'
import * as React from 'react'
import Counter from './Counter'
import AsyncComponent from './async-component'
import Portal from './Portal'

const importer = () => import('./DeferredRender')
const Async = () => <AsyncComponent importer={importer} />

const App = () => (
<h1>
<p>{40}!</p>
<Counter />
<Async />
<Portal />
</h1>
)

setConfig({ logLevel: 'debug' })

export default hot(module)(App)
29 changes: 29 additions & 0 deletions examples/async-portals/src/Counter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import React from 'react'

const RAND = Math.round(Math.random() * 1000)

class Counter extends React.Component {
state = { count: 0 }
gen = 0

componentDidMount() {
this.setState({
count: RAND,
})
}

componentWillUnmount() {
clearInterval(this.interval)
}

render() {
// gen should change. count - no.
return (
<span>
{this.state.count}:{this.gen++}
</span>
)
}
}

export default Counter
20 changes: 20 additions & 0 deletions examples/async-portals/src/DeferredRender.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import React from 'react'
import { Portal } from 'react-portal'
import hidden from './HiddenComponent'

const Hidden = hidden()

const APortal = () => (
<Portal>
This is a async portal
<Hidden.counter />
</Portal>
)

export default () => (
<div>
ASYNC
<Hidden.counter />
and <APortal />
</div>
)
14 changes: 14 additions & 0 deletions examples/async-portals/src/HiddenComponent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import React from 'react'
import Counter from './Counter'

const hidden = function() {
return {
counter: () => (
<div>
this is hidden counter(<Counter />)
</div>
),
}
}

export default hidden
16 changes: 16 additions & 0 deletions examples/async-portals/src/Portal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import React from 'react'
import { Portal } from 'react-portal'
import hidden from './HiddenComponent'

const Hidden = hidden()

const InPortal = ({ children }) => <div> + {children}</div>

export default () => (
<Portal>
<InPortal>
This is a first portal
<Hidden.counter />
</InPortal>
</Portal>
)
37 changes: 37 additions & 0 deletions examples/async-portals/src/async-component.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import React, { Component } from 'react'

class AsyncComponent extends Component {
constructor(props) {
super(props)
this.state = {}
}

componentWillMount() {
this.load()
}

componentWillReceiveProps() {
if (module.hot) {
setImmediate(() => {
this.load()
})
}
}

load() {
return this.props.importer().then(payload => {
this.setState({ AsyncComponent: payload.default })
})
}

render() {
const { AsyncComponent } = this.state

if (AsyncComponent) {
return <AsyncComponent {...this.props} />
}
return <div>async</div>
}
}

export default AsyncComponent
9 changes: 9 additions & 0 deletions examples/async-portals/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import 'babel-polyfill'
import React from 'react'
import { render } from 'react-dom'
import App from './App'

const root = document.createElement('div')
document.body.appendChild(root)

render(<App />, root)
22 changes: 22 additions & 0 deletions examples/async-portals/webpack.config.babel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* eslint-disable */
const path = require('path')
const webpack = require('webpack')
const HtmlWebpackPlugin = require('html-webpack-plugin')

module.exports = {
entry: ['./src/index'],
output: {
path: path.join(__dirname, 'dist'),
filename: 'bundle.js',
},
module: {
rules: [
{
//exclude: /node_modules|packages/, // should work without exclude
test: /\.js$/,
use: 'babel-loader',
},
],
},
plugins: [new HtmlWebpackPlugin(), new webpack.NamedModulesPlugin()],
}
Loading

0 comments on commit 4e8e617

Please sign in to comment.