Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sub render #830

Merged
merged 5 commits into from
Feb 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 = {}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have class properties 😁.

}

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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excluding node_modules is a good thing, please add it and remove comment.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As long CRA will not exclude node_modules and some projects might follow the same approach - we shall be ready (and we are ready).

test: /\.js$/,
use: 'babel-loader',
},
],
},
plugins: [new HtmlWebpackPlugin(), new webpack.NamedModulesPlugin()],
}
Loading