简体   繁体   English

OnClick 不工作,但控制台中没有出现错误

[英]OnClick is not working, but no errors appear in the console

As I am currently learning React, I am creating a joke generator.由于我目前正在学习 React,所以我正在创建一个笑话生成器。 I created a local file and was able to pull the data (jokes) and display it on the browser.我创建了一个本地文件并能够提取数据(笑话)并将其显示在浏览器上。 Now, I am trying to create a button that when you click, it displays a random joke.现在,我正在尝试创建一个按钮,当您单击该按钮时,它会显示一个随机笑话。 I can see the joke and button, but no action is being triggered.我可以看到笑话和按钮,但没有触发任何操作。 I check the console and there are no errors.我检查了控制台,没有错误。 If I use onClick = {randomJoke} , then I get an error saying listener was expecting a function, not an object. Can someone point help me out and point out what is wrong?如果我使用onClick = {randomJoke} ,那么我会收到一条错误消息,说听众期待的是 function,而不是 object。有人可以帮我指出问题所在吗?

This is how I currently have it set up:这就是我目前的设置方式:

import React from 'react'
import SportsJokesData from './SportsJokesData';

class SportsJokesApi extends React.Component {

    constructor(props){
        super(props);
        this.getRandomJoke = this.getRandomJoke.bind(this);
    }

    getRandomJoke(){
       return SportsJokesData[(SportsJokesData.length * Math.random()) << 0]

    }

    render() {
        const randomJoke =  this.getRandomJoke()
        return (
        <React.Fragment>
             <p> {randomJoke.question}</p>
            <p>{randomJoke.answer}</p>

        <button onClick={this.getRandomJoke}>
           click here
        </button>
        </React.Fragment>
        )
        }
    }
          export default SportsJokesApi;

This is how the file was originally scripted prior to adding the button.这是在添加按钮之前文件最初编写脚本的方式。

import React from 'react'
import SportsJokesData from './SportsJokesData';

class SportsJokesApi extends React.Component {

    getRandomJoke(){
       return SportsJokesData[(SportsJokesData.length * Math.random()) << 0]

    }

    render() {
        const randomJoke =  this.getRandomJoke()
        return (
            <React.Fragment>
            <p>{randomJoke.question}</p>
            <h1>{randomJoke.answer}</h1>
            </React.Fragment>           
        )
        }
    }
          export default SportsJokesApi;

You cannot just return a new component on onClick event.您不能只在onClick事件上return一个新组件。 You need to set some state first.您需要先设置一些 state。 This guide is useful: https://flaviocopes.com/react-show-different-component-on-click/本指南很有用: https://flaviocopes.com/react-show-different-component-on-click/

You need to re-render your React component, when there is a new joke.当出现新笑话时,你需要重新渲染你的 React 组件。 To do this store the randomJoke in the state of your React Component.为此,将randomJoke存储在 React 组件的state中。

When you call this.getRandomJoke you should update the state along with it, so that a re-render of the React component is triggered.当您调用this.getRandomJoke时,您应该同时更新state ,以便触发 React 组件的重新渲染。 When this happens the UI will be updated with the latest value of the randomJoke发生这种情况时,UI 将更新为randomJoke的最新值

 import React from 'react' import SportsJokesData from './SportsJokesData'; const initialState = { randomJoke: null }; class SportsJokesApi extends React.Component { constructor(props) { super(props); this.getRandomJoke = this.getRandomJoke.bind(this); this.state = initialState; } getRandomJoke() { this.setState({ randomJoke: SportsJokesData[(SportsJokesData.length * Math.random()) << 0] }); } render() { const { randomJoke } = this.state; return ( <React.Fragment > <p> {randomJoke.question} </p> <p> {randomJoke.answer} </p> <button onClick = {this.getRandomJoke}>click here </button> </React.Fragment > ); } } export default SportsJokesApi;

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

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