Skip to content

Commit 5b0c36f

Browse files
committed
Add react-faux-dom state post
1 parent 6d4819f commit 5b0c36f

File tree

6 files changed

+107
-0
lines changed

6 files changed

+107
-0
lines changed
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
layout: post
3+
title: react-faux-dom state
4+
---
5+
6+
I see a lot of people attempting to use [react-faux-dom][] with stateful D3 components (which doesn't work without modification) and then opening GitHub issues about the problem. This little example should hopefully clear things up around how to make stateful D3 components render through [react-faux-dom][].
7+
8+
Essentially, you can't just mutate the element, because there is no element to mutate inside the render function, it's React! So instead you have to use `setState` to get React to re-render you component with some new settings, you then use those settings to render the component with your desired modifications in place.
9+
10+
The example isn't that impressive, but [the source code][source] for it should explain a lot.
11+
12+
<div id="mount"></div>
13+
14+
Yes this isn't using things like D3 animation or brushing, but to be quite honest I'm not sure how I could get those to work very easily right now. If you want to animate things, use a React animation library (they're great and work fine with faux DOM), you have to find the React way to do things, sadly some D3 concepts just don't translate. If you want some physics based graph full of state then you're probably better off keeping to the original way of embedding D3 in React, dropping out of React and letting D3 mutate that element.
15+
16+
[react-faux-dom][] allows you to embed stateless D3 components into your React components (or vice versa) with ease, it's not designed to support every single stateful mutation D3 can throw at it. I have built an interactive graph with hover based tooltips which works great, physics and complex D3 plugins don't seem very easy to integrate with though.
17+
18+
<script src="/js/react-faux-dom-state/main.min.js"></script>
19+
20+
[react-faux-dom]: https://github.com/Olical/react-faux-dom
21+
[source]: https://github.com/Olical/lab/tree/gh-pages/js/react-faux-dom-state/

js/react-faux-dom-state/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules/

js/react-faux-dom-state/Makefile

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
default: bootstrap build
2+
3+
bootstrap:
4+
npm install
5+
6+
build:
7+
./node_modules/.bin/browserify ./main.js | ./node_modules/.bin/uglifyjs -c -m > main.min.js

js/react-faux-dom-state/main.js

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
var d3 = require('d3')
2+
var React = require('react')
3+
var ReactDOM = require('react-dom')
4+
var ReactFauxDOM = require('react-faux-dom')
5+
6+
var Box = React.createClass({
7+
getInitialState: function () {
8+
return {
9+
mouseOver: false
10+
}
11+
},
12+
render: function () {
13+
var self = this
14+
var svg = d3.select(ReactFauxDOM.createElement('svg'))
15+
.attr({
16+
width: 300,
17+
height: 300
18+
})
19+
20+
svg
21+
.append('rect')
22+
.attr({
23+
width: 300,
24+
height: 300,
25+
fill: this.state.mouseOver ? 'red' : 'green'
26+
})
27+
.on('mouseover', function () {
28+
self.setState({
29+
mouseOver: true
30+
})
31+
})
32+
.on('mouseout', function () {
33+
self.setState({
34+
mouseOver: false
35+
})
36+
})
37+
38+
return svg.node().toReact()
39+
}
40+
})
41+
42+
ReactDOM.render(React.createElement(Box), document.getElementById('mount'))

js/react-faux-dom-state/main.min.js

+16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/react-faux-dom-state/package.json

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "react-faux-dom-state",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "main.js",
6+
"dependencies": {
7+
"browserify": "^12.0.1",
8+
"d3": "^3.5.8",
9+
"react": "^0.14.2",
10+
"react-dom": "^0.14.2",
11+
"react-faux-dom": "^2.1.1",
12+
"uglifyjs": "^2.4.10"
13+
},
14+
"devDependencies": {},
15+
"scripts": {
16+
"test": "echo \"Error: no test specified\" && exit 1"
17+
},
18+
"author": "",
19+
"license": "ISC"
20+
}

0 commit comments

Comments
 (0)