gpt4 book ai didi

javascript - React JS - 未捕获的类型错误 : Cannot read property 'bind' of undefined

转载 作者:行者123 更新时间:2023-11-29 19:16:03 26 4
gpt4 key购买 nike

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

主 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>
);
}
};

topic-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>
);
}
};

single-topic-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. props 传递 onClick 回调

    <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

关于javascript - React JS - 未捕获的类型错误 : Cannot read property 'bind' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35290357/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com