【问题标题】:render function automatically trigger onClick props function issue in React?渲染功能自动触发 React 中的 onClick 道具功能问题?
【发布时间】:2019-03-21 19:51:13
【问题描述】:

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

我有父组件是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;

【问题讨论】:

    标签: javascript reactjs components jsx es6-modules


    【解决方案1】:

    在渲染用户箭头函数中使用点击函数时在子组件中更改此设置

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

    【讨论】:

      【解决方案2】:

      在你的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;
      

      【讨论】:

        猜你喜欢
        • 2021-12-20
        • 2019-02-02
        • 2019-08-22
        • 1970-01-01
        • 2022-12-12
        • 1970-01-01
        • 2021-08-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多