Skip to content

Commit 78f1597

Browse files
author
Enrico Loparco (eloparco)
committed
Add react example
1 parent 18fddf2 commit 78f1597

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed

demo/react.html

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>Gridstack.js React integration example</title>
7+
<link rel="stylesheet" href="demo.css" />
8+
<script src="../dist/gridstack.all.js"></script>
9+
10+
<!-- Scripts to use react inside html -->
11+
<script src="https://unpkg.com/react@16/umd/react.production.min.js"></script>
12+
<script src="https://unpkg.com/react-dom@16/umd/react-dom.production.min.js"></script>
13+
<script src="https://unpkg.com/babel-standalone@6.15.0/babel.min.js"></script>
14+
</head>
15+
16+
<body>
17+
<div id="root"></div>
18+
</body>
19+
20+
<script type="text/babel">
21+
class App extends React.Component {
22+
state = {
23+
count: 0,
24+
info: "",
25+
items: [
26+
{ x: 2, y: 1, height: 2 },
27+
{ x: 2, y: 4, width: 3 },
28+
{ x: 4, y: 2, width: 1 },
29+
{ x: 3, y: 1, height: 2 },
30+
{ x: 0, y: 6, width: 2, height: 2 },
31+
],
32+
};
33+
34+
componentDidMount() {
35+
// Provides access to the GridStack instance across the React component.
36+
this.grid = GridStack.init({
37+
float: true,
38+
cellHeight: "70px",
39+
minRow: 1,
40+
});
41+
42+
this.grid.on("dragstop", (event, element) => {
43+
const node = element.gridstackNode;
44+
this.setState({
45+
info: `you just dragged node #${node.id} to ${node.x},${node.y} – good job!`,
46+
});
47+
48+
// Clear the info text after a two second timeout.
49+
// Clears previous timeout first.
50+
window.clearTimeout(this.timerId);
51+
this.timerId = window.setTimeout(() => {
52+
this.setState({
53+
info: "",
54+
});
55+
}, 2000);
56+
});
57+
}
58+
59+
addNewWidget = () => {
60+
const node = this.state.items[this.state.count] || {
61+
x: Math.round(12 * Math.random()),
62+
y: Math.round(5 * Math.random()),
63+
width: Math.round(1 + 3 * Math.random()),
64+
height: Math.round(1 + 3 * Math.random()),
65+
};
66+
node.id = node.content = String(this.state.count);
67+
this.setState((prevState) => ({
68+
count: prevState.count + 1,
69+
}));
70+
this.grid.addWidget(node);
71+
};
72+
73+
render() {
74+
return (
75+
<div>
76+
<h1>How to integrate GridStack.js with React.js</h1>
77+
<p>
78+
As with any virtualDOM-based framework, you need to check if React
79+
has rendered the DOM (or any updates to it){" "}
80+
<strong>before</strong> you initialize GridStack or call its
81+
methods. As a basic example, check this component's{" "}
82+
<code>mounted</code> hook.
83+
</p>
84+
<p>
85+
If your app requires more complex render logic than the inline
86+
template in `addWidget`, consider&nbsp;
87+
<a href="https://github.com/gridstack/gridstack.js/tree/develop/doc#makewidgetel">
88+
makeWidget
89+
</a>
90+
&nbsp;to let React deal with DOM rendering.
91+
</p>
92+
<button type="button" onClick={this.addNewWidget}>
93+
Add Widget
94+
</button>
95+
{this.state.info}
96+
<section class="grid-stack"></section>
97+
</div>
98+
);
99+
}
100+
}
101+
102+
ReactDOM.render(<App />, document.getElementById("root"));
103+
</script>
104+
</html>

0 commit comments

Comments
 (0)