Skip to content

Commit b3d50da

Browse files
author
xhlulu
committed
Add lazy loading and fragments
- AsyncComponent.react.js: The exported component (inside template dir) - {{component_name}}.react.js: The real component - LazyLoader.js: The intermediate lazy loading function
1 parent 58976de commit b3d50da

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import React, {Component} from 'react';
2+
import PropTypes from 'prop-types';
3+
import { {{cookiecutter.component_name}} as RealComponent } from '../LazyLoader';
4+
5+
/**
6+
* ExampleComponent is an example component.
7+
* It takes a property, `label`, and
8+
* displays it.
9+
* It renders an input with the property `value`
10+
* which is editable by the user.
11+
*/
12+
export default class {{cookiecutter.component_name}} extends Component {
13+
render() {
14+
return (
15+
<React.Suspense fallback={null}>
16+
<RealComponent {...this.props}/>
17+
</React.Suspense>
18+
);
19+
}
20+
}
21+
22+
{{cookiecutter.component_name}}.defaultProps = {};
23+
24+
{{cookiecutter.component_name}}.propTypes = {
25+
/**
26+
* The ID used to identify this component in Dash callbacks.
27+
*/
28+
id: PropTypes.string,
29+
30+
/**
31+
* A label that will be printed when this component is rendered.
32+
*/
33+
label: PropTypes.string.isRequired,
34+
35+
/**
36+
* The value displayed in the input.
37+
*/
38+
value: PropTypes.string,
39+
40+
/**
41+
* Dash-assigned callback that should be called to report property changes
42+
* to Dash, to make them available for callbacks.
43+
*/
44+
setProps: PropTypes.func
45+
};
46+
47+
48+
export const defaultProps = {{cookiecutter.component_name}}.defaultProps;
49+
export const propTypes = {{cookiecutter.component_name}}.propTypes;

{{cookiecutter.project_shortname}}/src/lib/LazyLoader.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import React, {Component} from 'react';
2+
import PropTypes from 'prop-types';
3+
import {defaultProps, propTypes} from '../components/{{cookiecutter.component_name}}.react';
4+
5+
/**
6+
* ExampleComponent is an example component.
7+
* It takes a property, `label`, and
8+
* displays it.
9+
* It renders an input with the property `value`
10+
* which is editable by the user.
11+
*/
12+
export default class {{cookiecutter.component_name}} extends Component {
13+
render() {
14+
const {id, label, setProps, value} = this.props;
15+
16+
return (
17+
<div id={id}>
18+
ExampleComponent: {label}&nbsp;
19+
<input
20+
value={value}
21+
onChange={
22+
/*
23+
* Send the new value to the parent component.
24+
* setProps is a prop that is automatically supplied
25+
* by dash's front-end ("dash-renderer").
26+
* In a Dash app, this will update the component's
27+
* props and send the data back to the Python Dash
28+
* app server if a callback uses the modified prop as
29+
* Input or State.
30+
*/
31+
e => setProps({ value: e.target.value })
32+
}
33+
/>
34+
</div>
35+
);
36+
}
37+
}
38+
39+
40+
{{cookiecutter.component_name}}.defaultProps = defaultProps;
41+
{{cookiecutter.component_name}}.propTypes = propTypes;

0 commit comments

Comments
 (0)