React的onClick事件在页面渲染过后自动执行的问题

项目场景:

给LoadMore这个组件设置onClick事件,并用redux thunk 中间件让 action 创建函数返回一个函数。


问题描述

通过分析和调试发现的具体问题如下:
React的onClick事件在页面渲染过后自动执行的问题
组件代码:

import React, { Component } from "react";
import { Fragment } from "react";
import { ListItem, ListInfo, LoadMore } from '../style';
import { connect } from 'react-redux';
import * as actionCreator from '../store/actionCreator'
class ArticleList extends Component {
    constructor(props) {
        super(props);
    }
    render() {
        const { articleList, getMoreList } = this.props;
        return (
            <Fragment>
                {
                    articleList.map((item) => {
                        return (
                            <ListItem key={item.get('id')}>
                                <ListInfo>
                                    <h3 className="title">{item.get('title')}</h3>
                                    <p className="info">{item.get('info')}</p>
                                </ListInfo>
                                {/* <img src="https://upload-images.jianshu.io/upload_images/23868698-07ed2ab2a9c8a6c1.jpg?imageMogr2/auto-orient/strip|imageView2/1/w/360/h/240"></img> */}
                            </ListItem>
                        )
                    })
                }

                <LoadMore onClick={getMoreList()}>加载更多</LoadMore>

            </Fragment>
        )
    }
}
const mapStateToProps = (state) => {
    return {
        articleList: state.getIn(['homeReducer', 'articleList'])
    }
}

const mapDispatch = (dispatch) => (
    {
        getMoreList() {

            const action = actionCreator.getMoreListAction();
            console.log(action);
            dispatch(action);
        }
    }
)
export default connect(mapStateToProps, mapDispatch)(ArticleList);

原因分析:

未点击加载更多按钮就自动执行了该事件
在这里插入图片描述

<LoadMore onClick={getMoreList()}>加载更多</LoadMore>

:onClick事件如果包含 () ,就会变成执行函数,页面加载后会自动执行


解决方案:

提示:将代码纠正一下就可以了
此语法确保 getMoreList 内的 this 已被绑定。

<LoadMore onClick={() => { getMoreList() }}>加载更多</LoadMore>

或者

<LoadMore onClick={getMoreList.bind(this)}>加载更多</LoadMore>
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
React 中重新渲染当前页面有几种方式,下面是几个示例代码: 1. 使用 setState 方法: ``` class App extends React.Component { constructor(props) { super(props); this.state = { count: 0 }; this.handleClick = this.handleClick.bind(this); } handleClick() { this.setState({ count: this.state.count + 1 }); } render() { return ( <div> <p>Count: {this.state.count}</p> <button onClick={this.handleClick}>Click me</button> </div> ); } } ``` 当点击按钮,会调用 `handleClick` 方法并更新状态,从而重新渲染当前页面。 2. 使用 forceUpdate 方法: ``` class App extends React.Component { constructor(props) { super(props); this.state = { count: 0 }; this.handleClick = this.handleClick.bind(this); } handleClick() { this.setState({ count: this.state.count + 1 }); this.forceUpdate(); } render() { return ( <div> <p>Count: {this.state.count}</p> <button onClick={this.handleClick}>Click me</button> </div> ); } } ``` 当点击按钮,除了更新状态外,还会调用 `forceUpdate` 方法强制重新渲染当前页面。 3. 使用 key 属性: ``` class App extends React.Component { constructor(props) { super(props); this.state = { count: 0 }; this.handleClick = this.handleClick.bind(this); } handleClick() { this.setState({ count: this.state.count + 1 }); } render() { return ( <div key={this.state.count}> <p>Count: {this.state.count}</p> <button onClick={this.handleClick}>Click me</button> </div> ); } } ``` 当状态更新,重新渲染当前页面的关键在于 `key` 属性的变化,而不是直接重新渲染整个组件。因此,在组件的根元素中设置 `key` 属性并将其设置为状态的值,可以实现重新渲染当前页面的效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值