React JS - 在将数据放入DOM之后调用函数

时间:2016-08-31 08:36:59

标签: javascript reactjs

我想调用函数" activateGrid()"一旦DOM填充了数据。我知道它并不理想,但除了使用特定的jquery插件之外我别无选择。

我的问题是我不知道如何拨打电话" activateGrid()"所以互动都已到位。

我是否需要使用特定的生命周期方法?在DOM填充之后会发生什么?

以下是代码:

import React, { Component } from 'react';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import Header from '../components/header';
import { fetchPosts } from '../actions/index';
import { Link } from 'react-router';

class Dashboards extends Component {

  componentDidMount()
  {
    this.activateGrid();
  }

 componentDidUpdate()
 {
    this.activateGrid();
  }

  componentWillMount()
  {
    this.props.fetchPosts();

    this.addWidgetHandler = this.addWidgetHandler.bind(this);
    this.editDashboardHandler = this.editDashboardHandler.bind(this);
  }

        getItemNormalElement()
        {
          var $item = $('<div class="grid-item normal"><div class="title">UK Desk Occupancy</div><div class="image">New item</div></div>');

          return $item;
        }


        addWidgetHandler()
        {




            /* Add in new normal item START */
            var $items = this.getItemNormalElement();
              // append elements to container
              $('#grid').append( $items )
                // add and lay out newly appended elements
                .packery( 'appended', $items ).find($items).draggable();

                $('#grid').packery('bindUIDraggableEvents', $items);
            /* Add in new normal item END */





        }


        editDashboardHandler()
        {

        }






      draggingItem()
      {
          console.log("dragging in progress");

      }


      sortGrid()
      {
          console.log("complete");
          console.log(this);

          $('#grid').packery();

          /* Reload it a second time incase something disappears START */
          setTimeout(function(){ $('#grid').packery(); }, 3000);
          /* Reload it a second time incase something disappears END */

      }



      activateGrid()
      {



                     var $grid = $('#grid').packery({
                        itemSelector: '.grid-item',
                        gutter: 20,
                        columnWidth: 257
                    });
                    var $items = $grid.find('.grid-item.normal').draggable();
                    $grid.packery('bindUIDraggableEvents', $items);

                    $grid.on( 'layoutComplete', this.draggingItem );
                    $grid.on( 'dragItemPositioned', this.sortGrid );




      }





renderPosts() {
    return this.props.posts.map((post,i) => {
    console.log(post);
      return (

                        <div className="grid-item normal" key={i}>
                            <div className="title">{post.type}</div>
                            <div className="image">{post.name}</div>
                          </div>



      );
    }
    );




  }





  render() {
    return (
        <div>
        <Header />

            <div className="contentContainer">


                <div className="dashboardOptions">
                    <div className="option addWidget" onClick={this.addWidgetHandler}>Widget</div>
                    <div className="option editDashboard" onClick={this.editDashboardHandler}>Edit Dashboard</div>
                </div>

                <div className="mainSectionHeading">Create new report</div>







                        <hr />


                        <div className="grid" ref="gridContainer" id="grid">
                            {this.renderPosts()}
                        </div>

                        <hr />

            </div>


          </div>








    );
  }
}



function mapStateToProps(state) {
  return { posts: state.posts.all};
}

function mapDispatchToProps(dispatch){
  return bindActionCreators({ 
                              fetchPosts
                              }, dispatch);
}

export default connect(mapStateToProps, mapDispatchToProps)(Dashboards);

1 个答案:

答案 0 :(得分:0)

第一次有两个函数select * from t where R ,下一次有componentDidMount