简体   繁体   English

反应单击按钮以呈现页面

[英]React click button to render the page

In the renderList() , I have a delete button that will delete the content once it is clicked. renderList() ,我有一个删除按钮,一旦单击它就会删除它。 I am not sure where to put the setState so I put it inside on the onClick() . 我不知道在哪里放置setState所以我把它放在onClick() This doesn't work. 这不起作用。 I would like to know if I am doing this correct or if there is a better way to solve this. 我想知道我这样做是否正确,或者是否有更好的方法来解决这个问题。

onClick Function onClick函数

onClick={() => {
     this.props.deleteBook(list.book_id);
     this.setState({delete: list.book_id});
}}>

React.js React.js

import React, { Component } from 'react';
import { connect } from 'react-redux';
import { selectUser } from '../actions/index.js';
import { deleteBook } from '../actions/index.js';
import _ from 'lodash';

class myPage extends Component {
    constructor(props) {
        super(props);
        this.state = {
            delete: 0
        }
    }

    componentWillMount() {    
        this.props.selectUser(this.props.params.id);
    } 


    renderList() {
          return this.props.list.map((list) => {
            return (
                <li className='book-list'
                    key={list.book_id}>
                        {list.title}
                        <button
                        value={this.state.delete}
                        onChange={this.onClickChange} 
                        onClick={() => {
                            this.props.deleteBook(list.book_id);
                            this.setState({delete: list.book_id});
                        }}>
                        Delete
                        </button>

                </li>
            );
        })
    }

    render() {
        const {user} = this.props;
        const {list} = this.props;
        if(user) {

            return(
                <div>
                    <h2>Date Joined: {user.user.joined}</h2>
                    <h1>My Page</h1>
                    <h2>Username: {user.user.username}</h2>
                    <div>My Books:
                            <h1>
                                {this.renderList()}
                            </h1>
                    </div>
                </div>
            )
        }
    }
}
function mapStateToProps(state) {
    return {
        user: state.user.post,
        list: state.list.all
    }
}
export default connect(mapStateToProps, { selectUser, deleteBook })(myPage);

Based on your use of mapStateToProps , seems like you are using Redux. 基于您对mapStateToProps的使用,您似乎正在使用Redux。 Your list of books comes from the Redux store as props which is external to the component. 您的书籍列表来自Redux商店,作为组件外部的props

You do not need this.state.delete in the component. 组件中不需要this.state.delete As state is managed by Redux, it seems like the bug is in your Redux code and not React code. 由于状态由Redux管理,看起来好像是你的Redux代码中的错误,而不是React代码。 Look into the reducers and ensure that you are handling the delete item action correctly. 查看reducer并确保正确处理删除项操作。

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

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