React JS - Uncaught TypeError: Cannot read property 'bind' of undefined

React JS - Uncaught TypeError: Cannot read property 'bind' of undefined

我想在用户单击 SingleTopicBox 组件时显示 TopicsList 组件并隐藏 SelectedTopicPage 组件。但是,我收到错误:Uncaught TypeError: Cannot read property 'bind' of undefined 主题 - list.jsx 文件。

主-controller.jsx

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

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

export default class MainController extends React.Component {

  state = {
    isTopicClicked: false,
    topicPages
  };

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

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

主题-list.jsx

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

import SingleTopicBox from './single-topic-box.jsx';
import SelectedTopicPage from './selected-topic-page.jsx';
import topicPages from '../content/json/topic-pages.js';

export default class TopicsList extends React.Component {
  onClick(){
    this.props.onClick.bind(null, this.topicID);
  },
  render() {
    return (
      <div className="row topic-list">
        <SingleTopicBox topicID="1" onClick={this.onClick} label="Topic"/>
        <SingleTopicBox topicID="2" onClick={this.onClick} label="Topic"/>
        <SingleTopicBox topicID="3" onClick={this.onClick} label="Topic"/>
        <SingleTopicBox topicID="4" onClick={this.onClick} label="Topic"/>
      </div>
    );
  }
};

单题-box.jsx

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

export default class SingleTopicBox extends React.Component {

    render() {
        return (
            <div>
                <div className="col-sm-2">
                    <div className="single-topic" data-topic-id={this.props.topicID}>
                        {this.props.label} {this.props.topicID}
                    </div>
                </div>
            </div>
        );
    }
};

你有几个错误

  1. 您应该将 onClick 传递给 TopicsList

    render() {
      return (
        <div className="row">
          {this.state.isTopicClicked
            ? <SelectedTopicPage 
                 topicsID={this.state.topicsID} 
                 key={this.state.topicsID} 
                 topicPages={topicPages} />
            : <TopicsList onClick={ this.onClick.bind(this) } />}
        </div>
      );
    }
    
  2. TopicsList 中删除 onClick 方法

     onClick() {
       // ...
     },
    
  3. 通过 onClick 来自 props

    的回调
    <SingleTopicBox topicID="1" onClick={this.props.onClick} label="Topic"/>
    
  4. 添加到 SingleTopicBox onClick 事件

    <div 
       className="single-topic" 
       data-topic-id={this.props.topicID} 
       onClick={ () => this.props.onClick(this.props.topicID) }
    >
      {this.props.label} {this.props.topicID}
    </div>
    
  5. 你不需要调用 setState 两次

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

Example