reactjs 在prop drilling中遇到问题,似乎无法通过handleDelete传递参数

cgvd09ve  于 2023-05-22  发布在  React
关注(0)|答案(2)|浏览(115)

我对React Js相当陌生,似乎无法让我的头围绕 prop 钻孔,我这里的问题是,当我单击删除按钮时,我想控制台记录相应的卡ID,然后稍后过滤它们并更改状态......

  • 我确实尝试将id作为参数传递,但它会抛出一个错误,说handleDelete不是一个函数

App.jsx

import './App.css'
import { useState } from 'react'
import Header from './components/Header'
import FeedbackData from './data/FeedbackData'
import FeedbackList from './components/FeedbackList'
import FeedbackStats from './components/FeedbackStats'

function App() {
    const [feedback, setFeedback] = useState(FeedbackData)
    const handleDelete = (id) => {
        console.log(123)
    }
    return (
        <>
            <Header></Header>
            <div className='container col-12  col-sm-10 col-md-6 col-lg-7'>
                <FeedbackStats feedback={feedback}></FeedbackStats>
                <FeedbackList feedback={feedback} handleDelete={handleDelete}></FeedbackList>
            </div>
        </>
    )
}

export default App

FeedbackItem.jsx

import Card from './shared/Card'
function FeedbackItem({ item, handleDelete }) {
    return (
        <>
            <div className='feedback-item'>
                <Card item={item} handleDelete={handleDelete}></Card>
            </div>
        </>
    )
}

export default FeedbackItem

FeedbackList.jsx

function FeedbackList({ feedback, handleDelete }) {
    if (!feedback || feedback.length === 0) {
        console.log('no feedback')
    }

    return (
        <>
            {feedback.map((item) => (
                <FeedbackItem
                    key={item.id}
                    item={item}
                    handleDelete={handleDelete}></FeedbackItem>
            ))}
        </>
    )
}

export default FeedbackList

card.jsx

import { FaTimes, FaEdit } from 'react-icons/fa'
function Card({ item, handleDelete }) {
    return (
        <>
            <div className='card'>
                <div className='card-body'>
                    <div className='rating-badge' id='rating'>
                        {item.rating}
                    </div>
                    <div className='buttons'>
                        <button className='btn'>
                            <FaEdit></FaEdit>
                        </button>
                        <button onClick={handleDelete} className='btn'>
                            <FaTimes></FaTimes>
                        </button>
                    </div>
                    <p className='card-text'>{item.feedbackText}</p>
                </div>
            </div>
        </>
    )
}

export default Card
lawou6xi

lawou6xi1#

这里我假设你想把参数从组件传递给handleDelete()函数。
在卡组件中:

import { FaTimes, FaEdit } from 'react-icons/fa'
function Card({ item, handleDelete }) {
    return (
        <>
            <div className='card'>
                <div className='card-body'>
                    <div className='rating-badge' id='rating'>
                        {item.rating}
                    </div>
                    <div className='buttons'>
                        <button className='btn'>
                            <FaEdit></FaEdit>
                        </button>
                        <button onClick={()=>handleDelete("123")} className='btn'>
                            <FaTimes></FaTimes>
                        </button>
                    </div>
                    <p className='card-text'>{item.feedbackText}</p>
                </div>
            </div>
        </>
    )
}

export default Card

这里:

onClick={()=>handleDelete("123")}

当你试图将函数作为接受参数的props传递时。然后你必须在arrow函数中调用你的函数,参数如上所示。
如果要从App.js组件传递参数:

<FeedbackList feedback={feedback} handleDelete={()=>handleDelete("123")}></FeedbackList>

注意:通过使用Context API和Hooks,您可以避免prop drilling,并直接访问任何需要它的组件中的共享数据,使您的应用程序更易于维护和重构。
如果有什么不对劲就告诉我。

txu3uszq

txu3uszq2#

如果你要传递一个参数给一个事件处理器并绑定它,你必须使用下面的语法;

handleDelete={() => handleDelete(id)}

为了比较,

This will fire your event when it's clicked
onClick={handleDelete}

This will fire your event when it's clicked with the given arguments
onClick={() => handleDelete(id)}

This will fire ALWAYS. Even if you don't click it. Because you're not binding the function, 
you are calling it.
onClick={handleDelete(id)}

相关问题