渲染功能会自动触发React中的onClick道具功能问题?

时间:2018-10-17 07:45:05

标签: javascript reactjs components jsx es6-modules

在父组件中有一个子组件。在儿童部分,我有一个带有点击功能的道具。在该单击功能中,在渲染组件时单击之前触发。我需要限制渲染中的单击功能。检查下面我的代码...

我的父组件是APP。

    import React, { Component } from "react";
import { withRouter } from 'react-router-dom';
import { connect } from "react-redux";

import { Pagination } from './../Common/index';

class App extends Component {

  handlePageination = (current) => {
    console.log('trigger')
  }

  render() {
    return (
          <main className="main-block">
            <Pagination total={userList.length>0 ? totalCount : 0} range={10} initial={0} rowClick={this.handlePageination} count={currentPageNumber} />
          </main>

    )
  }
}

export default App;

子组件...

import React, { Component } from "react";
import ReactPaginate from 'react-paginate';
import { ORIGIN_PATH } from "./../../../utilities/consts";
import "./Pagination.css";

class Pagination extends Component {

    handlePageClick = (e) => {
        this.props.rowClick(e.selected)
    }

    render() {

        const { range, initial } = this.props;

        let { count, total } = this.props;

        count = count + 10;
        total = parseInt(total, 10);

        if(count===0 && total < 10) {
            count = total
        } else if(count >= total) {
            count = total
        }

        return(
            <div className="pagination-block">
                <p>Showing {count} out of {total} results</p>
                {<ReactPaginate previousLabel={<img src={ORIGIN_PATH + "/images/icons/polygon-prev-icon@3x.png"} alt="Prev" />}
                    nextLabel={<img src={ORIGIN_PATH + "/images/icons/polygon-next-icon@3x.png"} alt="Next" />}
                    breakLabel={<a href="">...</a>}
                    breakClassName={"break-me"}
                    pageCount={total/range}
                    marginPagesDisplayed={2}
                    pageRangeDisplayed={range}
                    onPageChange={this.handlePageClick}
                    containerClassName={"pagination"}
                    subContainerClassName={"pages pagination"}
                    initialPage={initial}
                    activeClassName={"active"} />}
            </div>
        )
    }
}

export default Pagination;

2 个答案:

答案 0 :(得分:0)

在渲染用户箭头功能内使用click功能时,在子组件中更改此设置

onPageChange={(e) => this.handlePageClick()}

答案 1 :(得分:0)

在您的Pagination组件中渲染

更改

 onPageChange={this.handlePageClick}

收件人

 onPageChange={e => this.handlePageClick(e)}

下面的更正代码

import React, { Component } from "react";
import ReactPaginate from 'react-paginate';
import { ORIGIN_PATH } from "./../../../utilities/consts";
import "./Pagination.css";

class Pagination extends Component {

    handlePageClick = (e) => {
        this.props.rowClick(e.selected)
    }

    render() {

        const { range, initial } = this.props;

        let { count, total } = this.props;

        count = count + 10;
        total = parseInt(total, 10);

        if(count===0 && total < 10) {
            count = total
        } else if(count >= total) {
            count = total
        }

        return(
            <div className="pagination-block">
                <p>Showing {count} out of {total} results</p>
                {<ReactPaginate previousLabel={<img src={ORIGIN_PATH + "/images/icons/polygon-prev-icon@3x.png"} alt="Prev" />}
                    nextLabel={<img src={ORIGIN_PATH + "/images/icons/polygon-next-icon@3x.png"} alt="Next" />}
                    breakLabel={<a href="">...</a>}
                    breakClassName={"break-me"}
                    pageCount={total/range}
                    marginPagesDisplayed={2}
                    pageRangeDisplayed={range}
                    onPageChange={e => this.handlePageClick(e)}
                    containerClassName={"pagination"}
                    subContainerClassName={"pages pagination"}
                    initialPage={initial}
                    activeClassName={"active"} />}
            </div>
        )
    }
}

export default Pagination;