在React中调用第三方Lib函数

Groovietunes

我在React中使用d3,并具有一些与d3相关的功能,这些功能需要访问一些d3变量/对象。调用componentWillReceiveProps()时,我需要调用这些函数之一。我提供了一个最小的示例,最重要的是注释。

class App extends Component {
   constructor() {
      super();
   }

   shouldComponentUpdate() {
      return false;
   }

   componentWillReceiveProps(nextProps) {
      /* Need to call reset() here */
   }

   componentDidMount() {
       let svg = d3.select(this.refs.map).append('svg')
            .attr('width', width)
            .attr('height', height);

       let g = svg.append('g')
            .style('stroke-width', '.4px');

       function reset() {
          /* needs access to g */
          g.transition()
                .duration(1500)
                .style('stroke-width', '.4px')
                .attr('transform', '');
       }
   }

   render() {
        return (
            <div id="map" ref="map"></div>
        )
    }
}
加莱尔·巴莎
  • reset()功能定义为组件方法
  • 绑定功能this参考reset()
  • 用于this.g获取g的值,以在不同的组件方法中使用它。

样例代码:

class App extends React.Component {
    constructor() {
      super();
      this.reset = this.reset.bind(this)
    }

    shouldComponentUpdate() {
      return false;
    }

    componentWillReceiveProps(nextProps) {
      this.reset();
    }

    componentDidMount() {
      let svg = d3.select(this.refs.map).append('svg')
        .attr('width', '100px')
        .attr('height', '100px');


      // Assigning value to `this.g` to be able use it in other methods.
      this.g = svg.append("circle")
        .attr("cx", 25)
        .attr("cy", 25)
        .attr("r", 25)
        .style("fill", "purple");
    }

    reset() {
      // After component will be mounted, you would have needed value in `this.g`.
      this.g.transition()
        .duration(1500)
        .style("fill", "red");
    }

    render() {
        return ( < div >
          < div id = "map"
          ref = "map" > < /div>
      <button onClick={this.reset}>Reset</button >
          < /div>
    )
  }
}

ReactDOM.render(<App / > , document.getElementById('app'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<div id="app"></div>

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在React组件中公开第三方lib(Firepad)方法

来自分类Dev

防止第三方函数中的exit()退出?

来自分类Dev

如何在ReactJS中调用第三方javascript函数

来自分类Dev

在angular指令中使用第三方lib

来自分类Dev

如何在由第三方库调用的回调函数中传递/使对象/变量可访问?

来自分类Dev

通过 Firebase Cloud 函数调用第三方 API(使用 Blaze 计划)。反应/还原

来自分类Dev

针对第三方HTTP调用优化Celery

来自分类Dev

如何从Elixir调用第三方Erlang模块?

来自分类Dev

如何通过Firebase调用第三方API?

来自分类Dev

调用第三方API时处理CORS

来自分类Dev

React:使用 Ajax 调用第三方 API (Behance)

来自分类Dev

第三方库中未调用全局重载delete []

来自分类Dev

如何使用第三方脚本中的导入正确调用js模块

来自分类Dev

如何模拟第三方React Native NativeModules?

来自分类Dev

在第三方 API 函数中传递两个列表

来自分类Dev

从Django中的第三方应用收集消息

来自分类Dev

在Pelican中安装第三方Markdown扩展

来自分类Dev

第三方键盘中的resignFirstResponder()

来自分类Dev

获取Java中的第三方库版本

来自分类Dev

伪造第三方库中的扩展方法

来自分类Dev

Chrome审核:第三方代码中的Datacamp

来自分类Dev

在Flutter中编辑第三方插件

来自分类Dev

在厨师中包括第三方资源

来自分类Dev

在Prestashop中安装第三方库

来自分类Dev

在WCF中捕获第三方DLL事件

来自分类Dev

EJB-JAR中的第三方库

来自分类Dev

prestashop上mailalert中的第三方模块

来自分类Dev

Google登录中的第三方图标

来自分类Dev

避免在Veracode中扫描第三方库