简体   繁体   English

React Parent to Children 事件处理值传递

[英]React Parent to Children event Handing value passing

I have two component我有两个组件
Read Mode and Pagination阅读模式和分页

Read Mode Component读取模式组件

     state = {
            currentPdf:[],
            currentPage: null,
            totalPages: null,
            intialState:1,
        };
        constructor(props) {
            super(props);
            this.onChangePage = this.onChangePage.bind(this);
            this.onCurrentPageNo = this.onCurrentPageNo.bind(this);
            this.onTotalPage = this.onTotalPage.bind(this);

        }
        componentDidMount() {
            this.props.fetchPdfContent();
        }
        onCurrentPageNo(currentPageNo){
            this.setState({ currentPage: currentPageNo });
        }
        onChangePage(currentPdf) {
            this.setState({ currentPdf: currentPdf });
        }
        onTotalPage(totalpages){
            this.setState({ totalPages: totalpages });
        }
        gotoPrevPage = (currentTarget) => {
            if (this.state.currentPage > 1) {
                let prevPage = this.state.currentPage - 1;
                this.setState({ intialState: prevPage });
            }
        }

     render() {
       return (
          <button className="btn btn-primary prev-btn pageBtn" onClick={this.gotoPrevPage.bind(this)}>
                 <span aria-hidden="true" className="icon-ico_arrow-right icomoon"> Left </span>
          </button>
          <Pagination initialPage={intialState} items={pdfcontnet} onTotalPage={this.onTotalPage} onChangePage={this.onChangePage} onChangePageNo={this.onCurrentPageNo} />
        )
      }

Pagination Component分页组件

     constructor(props) {
            super(props);
            this.state = { pager: {} };
        }
        componentDidMount() {
            // set page if items array isn't empty
            if (this.props.items && this.props.items.length) {
                this.setPage(this.props.initialPage);
            }
        }
        componentDidUpdate(prevProps, prevState) {
            // reset page if items array has changed
            this.setPage(this.props.initialPage);
        }
        setPage(page) {
            console.log(page + 'pages');
            var items = this.props.items;
            var pager = this.state.pager;
            if (page < 1 || page > pager.totalPages) {
                return;
            }
            // get new pager object for specified page
            pager = this.getPager(items.length, page);
            // get new page of items from items array
            var pageOfItems = items.slice(pager.startIndex, pager.endIndex + 1);
            // update state
            this.setState({ pager: pager });
            // call change page function in parent component
            this.props.onChangePage(pageOfItems);
        }

when i click the gotoPrevPage () initalState value need to pass当我点击 gotoPrevPage () initalState 值需要通过

this.setPage(this.props.initialPage);

if i assign componentDidUpdate() state I got如果我分配 componentDidUpdate() state 我得到了

克罗

try this:尝试这个:

onClick={()=>this.gotoPrevPage.bind(this)}

You need to change initialPage={intialState} to initialPage={this.state.intialState} in your Read Mode Component render method.您需要在读取模式组件渲染方法中将initialPage={intialState}更改为initialPage={this.state.intialState}

PS: You should actually spell it initial , not intial . PS:您实际上应该拼写为initial ,而不是intial

Your state object should be inside your constructor function in ReadMode.您的 state object 应该在 ReadMode 的构造函数 function 中。 You should also be using this.setState({}) to update state.您还应该使用this.setState({})来更新 state。 You also shouldn't be trying to reload the entire DOM when a new item is added the way you are in the Pagination component via commponentDidUpdate .当您通过commponentDidUpdate在分页组件中添加新项目时,您也不应该尝试重新加载整个 DOM。 React uses a virtual DOM and doesn't need to reload every element on the page every time one element is updated. React 使用virtual DOM ,不需要在每次更新一个元素时重新加载页面上的每个元素。 It looks like part of your problem is your App continuously updates and you're getting stuck in a never ending loop updating the DOM.看起来您的问题的一部分是您的应用程序不断更新,而您陷入了更新 DOM 的永无止境的循环中。

Also, you might not need Pagination to have local state.此外,您可能不需要分页来拥有本地 state。 You could store the state/data in the parent container and just pass it to Pagination via props .您可以将状态/数据存储在父容器中,然后通过props将其传递给 Pagination。 React uses one-way data flow and you should familiarize yourself with passing props from a parent component to a child component. React 使用单向数据流,您应该熟悉将props从父组件传递到子组件。

Brush up on state and lifecycle functions by reading the documentation , get an understanding of how React uses the virutal DOM to update elements, and rewrite your App so that state is stored mainly in the parent component and is passed via props to the child component.通过阅读文档statelifecycle functions ,了解 React 如何使用virutal DOM更新元素,并重写您的 App,使state主要存储在父组件中,并通过props传递给子组件。

Overlooking basic React concepts and structuring your project in a less than ideal way is the source of your problems.忽略基本的 React 概念并以不太理想的方式构建项目是问题的根源。

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

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