javascript - 如何在 React 中从子组件设置状态

标签 javascript reactjs

我想从子组件设置父组件的状态。我尝试使用 props 但它给出错误 Uncaught TypeError: this.props.setTopicClicked is not a function。有没有比使用 props 更有效的方法来设置父组件的状态?我想设置 isTopicClicked: true

的状态

主 Controller .jsx

import {React, ReactDOM} from '../../../build/react';

import SelectedTopicPage from '../selected-topic-page.jsx';
import TopicsList from '../topic-list.jsx';
import topicPageData from '../../content/json/topic-page-data.js';

export default class MainController extends React.Component {

  state = {
    isTopicClicked: false,
    topicPageData
  };

  onClick(topicID) {
    this.setState({
       isTopicClicked: true,
       topicsID: topicID
    });
  };

  setTopicClicked(event){
    this.setState({isTopicClicked: event});
  };

  render() {
    return (
      <div className="row">
        {this.state.isTopicClicked
          ? <SelectedTopicPage topicsID={this.state.topicsID} key={this.state.topicsID} topicPageData={topicPageData}/>
        : <TopicsList onClick={ this.onClick.bind(this) }/>}
      </div>
    );
  }
};

selected-topic-page.jsx

import {React, ReactDOM} from '../../build/react';

import SelectedTopicPageMarkup from './selected-topic-page-markup.jsx';
import NextPrevBtn from './next-prev-btn.jsx';

export default class SelectedTopicPage extends React.Component {
    state = {
        topicPageNo: 0,
        total_selected_topic_pages: 1
      };
    navigateBack(topicPageNo) {
        if (this.state.topicPageNo > 0){
            topicPageNo = this.state.topicPageNo - 1;
        }
        else {
            topicPageNo = 0;
        }
        this.setState({topicPageNo : topicPageNo});
    };
    navigateNext(totalPagesInSelectedTopic) {
        let topicPageNo;
        if (totalPagesInSelectedTopic > this.state.topicPageNo + 1){
            topicPageNo = this.state.topicPageNo + 1;
        }
        else if (totalPagesInSelectedTopic == this.state.topicPageNo + 1) {
          this.props.setTopicClicked(true);
        }
        else {
            topicPageNo = this.state.topicPageNo;
        }
        this.setState({topicPageNo : topicPageNo});
    };
    render() {
        let topicsID = this.props.topicsID;
        let topicPageNo = this.state.topicPageNo;
        return (
            <div>
                {this.props.topicPageData.filter(function(topicPage) {
                    // if condition is true, item is not filtered out
                    return topicPage.topic_no === topicsID;
                }).map(function (topicPage) {
                    let totalPagesInSelectedTopic = topicPage.topic_pages.length;
                    return (
                        <div>
                            <div>
                            <SelectedTopicPageMarkup headline={topicPage.topic_pages[0].headline} key={topicPage.topic_no}>
                                {topicPage.topic_pages[topicPageNo].description}
                            </SelectedTopicPageMarkup>
                            </div>
                            <div>
                                <NextPrevBtn moveNext={this.navigateNext.bind(this, totalPagesInSelectedTopic)} key={topicPage.topic_no} moveBack={this.navigateBack.bind(this, topicPageNo)}/>
                            </div>
                        </div>
                    );
                }.bind(this))}
            </div>
        );
    };
};

最佳答案

您似乎忘记将 setTopicClicked 传递给子项:

setTopicClicked={this.setTopicClicked.bind(this)}

关于javascript - 如何在 React 中从子组件设置状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35294765/

相关文章:

javascript - React - chop Markdown

javascript - 引导模式 : is not a function

javascript - Webpack babel-loader 不解析箭头函数

javascript - "this.state"或 Reactjs 中无法访问方法

javascript - 用于禁用按钮的两个逻辑运算符

javascript - 从状态加载数据时,React 组件不会被渲染

javascript - D3.js,加载数据成功后, View 中看不到任何数据

javascript - 运行简单递归函数时的最大调用堆栈大小错误

javascript - 仅在移动设备中添加 javascript 标签

reactjs - 当 Apollo 获取的数据通过 React 和 GraphQL 准备就绪时