简体   繁体   English

使用react和d3从外部json获取数据

[英]get data from external json with react and d3

I am brand new to React. 我是React的新手。 Am using v0.13.3. 我正在使用v0.13.3。 I have a functioning page (index.html with link to external css file and external js file). 我有一个正常运行的页面(带有指向外部css文件和外部js文件的链接的index.html)。 All works great. 一切都很好。

JS file contains exactly what is in the JavaScript pane of this JSFIDDLE . JS文件完全包含此JSFIDDLE的JavaScript窗格中的内容 (Apologies the fiddle doesn't run, I can't figure out why console says Uncaught SyntaxError: Unexpected token < . I have /** @jsx React.DOM */ at the top, but anyway...it makes a chart.) (抱歉,提琴无法运行,我无法弄清楚为什么控制台说Uncaught SyntaxError: Unexpected token < 。我在顶部有/** @jsx React.DOM */ ,但是无论如何...它制作了一张图表。)

QUESTION: How can I get what is in 问题:我怎么得到里面的东西

var data = [{"letter":"A","frequency":0.08167},"letter":"B","frequency":0.01492},{"letter":"C","frequency":0.02782},{"letter":"D","frequency":0.04253},...]

to come from an external JSON file instead? 而是来自外部JSON文件? (I want the chart to update if the JSON file's contents change). (如果JSON文件的内容发生更改,我希望图表进行更新)。

(I realize there is the option to use React to break all of the pieces of the d3 chart into separate components and do it that way instead of all of the d3 inside one function. But for now, I'd like it to remain this way - based on the conclusion of this article .) (我意识到,可以使用React将d3图表的所有部分分解为单独的组件,并以此方式代替一个函数中的所有d3。但是现在,我希望它保持这种状态方式-基于本文的结论。)

So, it didn't work to use d3.json(path/to/file.js) inside function createChart . 因此,在function createChart使用d3.json(path/to/file.js) And I've tried what is in this tutorial under "Hook Up the Data Model" and "Fetching from the server", but I'm not having any luck. 我已经尝试了本教程中的 “挂钩数据模型”和“从服务器获取”下的内容,但是我没有任何运气。 Also, looked at this article . 另外,看了这篇文章 I'm suspecting I need to do something else with componentDidMount and shouldComponentUpdate , but a bit baffled. 我怀疑我需要对componentDidMountshouldComponentUpdate做其他事情,但是有点困惑。 Searched for other examples, but having trouble finding one like this. 搜索了其他示例,但找不到这样的示例。 Thanks for any suggestions. 感谢您的任何建议。

One issue with the code in your JSFiddle is your use of the shouldComponentUpdate function. JSFiddle中代码的一个问题是您应该使用shouldComponentUpdate函数。 As per https://facebook.github.io/react/docs/component-specs.html this should only return a boolean telling React whether to re-render and not try to update the DOM itself. 按照https://facebook.github.io/react/docs/component-specs.html的规定,这仅应返回一个布尔值,告诉React是否重新渲染,而不是尝试更新DOM本身。 For responding to updates to props you would probably use componentDidUpdate in this case. 为了响应对道具的更新,在这种情况下,您可能会使用componentDidUpdate

Generally if you need to do anything asynchronous in a React component, it should happen in the componentDidMount function, so you're on the right path there. 通常,如果您需要在React组件中执行任何异步操作,则应在componentDidMount函数中进行,因此您在正确的位置。

On other I would try is in createChart() you have d3.select("body") - you could select the <div> that you pass in from React as the dom prop and append the <svg> directly there, which would be a much more appropriate way of using React (Components should be self-contained, so they wouldn't go around modifying the DOM outside of themselves. Try d3.select(dom).append('svg') . 在其他方面,我尝试在createChart()使用d3.select("body") -您可以选择从React作为dom createChart() d3.select("body")<div> ,然后直接在其中附加<svg>一种使用React的更合适的方法(组件应该是独立的,因此它们不会在自己外部修改DOM。请尝试d3.select(dom).append('svg')

Hope this is helpful! 希望这会有所帮助!

PS: I've had some good results using this Plunkr template to create React snippets: http://plnkr.co/edit/tpl:a3vkhunC1Na5BG6GY2Gf?p=preview PS:使用此Plunkr模板创建React片段时,我取得了一些不错的效果: http ://plnkr.co/edit/tpl:a3vkhunC1Na5BG6GY2Gf?p=preview

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM