【问题标题】:How to get React onClick to work? (Doesn't work even with arrow function)如何让 React onClick 工作? (即使使用箭头功能也不起作用)
【发布时间】:2017-07-25 20:09:27
【问题描述】:

即使在 SO 上的其他问题中找到建议后,我似乎仍然无法让 onClick 函数 this.changeContent 正常工作:

import React from 'react';
import Maxim from './Maxim';
import Challenge from './Challenge';
import Prompt from './Prompt';

class ContentContainer extends React.Component {
    constructor() {
        super();
        this.changeContent = this.changeContent.bind(this);
        this.state = {
            content: "maxim"
        }
    }

    changeContent(event) {
        console.log(this);
        if (this.state.content == "maxim") {
            this.setState({ content: "challenge" })
        } else {
            this.setState({ content: "maxim" })
        }
    }

    render() {
        let renderedContent = null;

        if (this.state.content == "challenge") {
            renderedContent = <Challenge content="The trick to presence is to listen to the space between the sounds." />;
        } else {
            renderedContent = <Maxim quote="Silence can be heard in every sound. All you need is to listen." />;
        }
            return (
                <div className="content-container">
                    {renderedContent}
                    <Prompt onClick={this.changeContent}/>
                </div>
            );
    }
}

export default ContentContainer;

我尝试过使用bind、箭头函数(如这里)、普通函数参考...几乎所有我在 SO 和其他地方看到的变体。我最初只有this.changeContent(),并且在页面加载时调用了该函数。我至少已经解决了这个问题,但这个函数仍然没有被调用——this.state.content 没有改变,控制台日志this 也没有。

谢谢。

【问题讨论】:

  • 您发布的代码看起来不错。如果它不起作用,那么问题可能出在其他地方。请发布所有相关信息。
  • 是的,如下所述,是 &lt;Prompt/&gt; 本身缺少导致问题的函数引用。

标签: javascript reactjs onclick ecmascript-6 arrow-functions


【解决方案1】:

检查这个工作示例,以类似的方式编写它:

class ContentContainer extends React.Component {
    constructor() {
        super();
        this.changeContent = this.changeContent.bind(this);
        this.state = {
            content: "maxim"
        }
    }

    changeContent(event) {
        let content = this.state.content;
        this.setState({ content: content == "challenge" ? "maxim" :  "challenge"})
    }

    render() {
        let renderedContent = null;

        if (this.state.content == "challenge") {
            renderedContent = <Challenge content="The trick to presence is to listen to the space between the sounds." />;
        } else {
            renderedContent = <Maxim quote="Silence can be heard in every sound. All you need is to listen." />;
        }
        return (
             <div className="content-container">
                 {renderedContent}
                 <Prompt onClick={this.changeContent}/>
             </div>
        );
    }
}

class Challenge extends React.Component{
   render(){
     return(
        <div>Inside Challenge<br/>{this.props.content}</div>
     )
   }
}

class Maxim extends React.Component{
   render(){
     return(
        <div>Inside Maxim<br/>{this.props.quote}</div>
     )
   }
}

class Prompt extends React.Component{
   render(){
     return(
        <div style={{paddingTop: '100px'}}>
           Inside Prompt<br/>
           <span onClick={()=>this.props.onClick()}>*Click Me to change the Component</span>
        </div>
     )
   }
}

ReactDOM.render(<ContentContainer/>, document.getElementById('app'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>

<div id='app'/>

【讨论】:

  • OK 完美,所以问题是我不是指实际 组件本身中的 onClick。愚蠢的错误。谢谢!
  • 你能解释一下你改变了什么以及为什么?
  • @FelixKling 是的,我在&lt;Prompt/&gt; 组件内添加了函数引用;以前我只有里面的文字。
  • @FelixKling 我向他展示了他所描述的基本流程,组件应如何连接以及如何在单击按钮时呈现不同的组件。
【解决方案2】:

<Prompt onClick={this.changeContent.bind(this)}/> 如果你还没有试试这个

【讨论】:

    猜你喜欢
    • 2017-06-29
    • 2020-12-11
    • 2016-11-03
    • 1970-01-01
    • 2021-10-10
    • 2017-02-09
    • 2019-09-30
    • 2020-07-31
    • 1970-01-01
    相关资源
    最近更新 更多