第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

React 多個(gè)孩子發(fā)送相同的道具

React 多個(gè)孩子發(fā)送相同的道具

慕無忌1623718 2023-03-03 13:09:08
在我的 React 應(yīng)用程序中,我從 api 獲取數(shù)據(jù)并在主頁中列出所有帖子,因此我的目的是創(chuàng)建一個(gè)函數(shù)來為帖子評(píng)分。因此,我正在映射所有帖子并將帖子信息傳遞給Rate組件,并采取ratePost行動(dòng)對(duì)帖子進(jìn)行評(píng)分。行動(dòng)export const ratePost = (rate, postId) => dispatch => {    const config = {        withCredentials: true    }    const sRate = parseInt(rate, 10)    const body = JSON.stringify({ star_rate:sRate })    axios.put(`http://127.0.0.1:8000/api/v1/p/rate/${postId}`, body, config)        .then(res => {            dispatch({                type: RATE_POST,                sRate,                postId            })        }).catch(err => {            console.log(err)            dispatch({                type: RATE_POST_FAIL            })        })}但是問題是,當(dāng)我發(fā)送操作時(shí),它總是發(fā)送相同的內(nèi)容postId,并且在控制臺(tái)記錄道具后,props即使我對(duì)不同的帖子進(jìn)行評(píng)分,它也顯示相同。在 React DevTools Components 部分,它顯示了多個(gè)Rate組件,它們的道具與預(yù)期的不同。(我刪除了代碼的不相關(guān)部分)export function Card(props) {    const { category } = useParams()        useEffect(() => {        if(!category){            props.getPosts()        } else {            props.getPostsByCategory(category)        }    },[category])    return (        <Fragment>            <div className="posts">                {props.posts.map(post =>                     <article key={post.id} id={post.id}>                        <div className="post-meta">                            <div className="stars">                                <Rate post={post}/>                            </div>                        </div>                    </article>                )}            </div>        </Fragment>    )    }費(fèi)率.jsexport function Rate(props) {    const onInput = (e) => {        props.ratePost(e.target.value, props.post.id)        console.log(props.post) /* shows the same props */    }export default connect(null, { ratePost })(Rate)
查看完整描述

1 回答

?
天涯盡頭無女友

TA貢獻(xiàn)1831條經(jīng)驗(yàn) 獲得超9個(gè)贊

經(jīng)過長(zhǎng)時(shí)間的研究,問題出id在inputs. 它們需要是獨(dú)一無二的,所以我像那樣更新了我的代碼=>


return(

    <fieldset className="rating" >

        <input type="button" onClick={onClick} id={"star5"+props.post.id} name="rating" value="5" /><label className = "full" htmlFor={"star5"+props.post.id}></label>

        <input type="button" onClick={onClick} id={"star4half"+props.post.id} name="rating" value="4.5" /><label className="half" htmlFor={"star4half"+props.post.id} ></label>

        <input type="button" onClick={onClick} id={"star4"+props.post.id} name="rating" value="4" /><label className = "full" htmlFor={"star4"+props.post.id} ></label>

        <input type="button" onClick={onClick} id={"star3half"+props.post.id} name="rating" value="3.5" /><label className="half" htmlFor={"star3half"+props.post.id} ></label>

        <input type="button" onClick={onClick} id={"star3"+props.post.id} name="rating" value="3" /><label className = "full" htmlFor={"star3"+props.post.id} ></label>

        <input type="button" onClick={onClick} id={"star2half"+props.post.id} name="rating" value="2.5" /><label className="half" htmlFor={"star2half"+props.post.id} ></label>

        <input type="button" onClick={onClick} id={"star2"+props.post.id} name="rating" value="2" /><label className = "full" htmlFor={"star2"+props.post.id} ></label>

        <input type="button" onClick={onClick} id={"star1half"+props.post.id} name="rating" value="1.5" /><label className="half" htmlFor={"star1half"+props.post.id} ></label>

        <input type="button" onClick={onClick} id={"star1"+props.post.id} name="rating" value="1" /><label className = "full" htmlFor={"star1"+props.post.id} ></label>

        <input type="button" onClick={onClick} id={"starhalf"+props.post.id} name="rating" value="0.5" /><label className="half" htmlFor={"starhalf"+props.post.id}></label>

    </fieldset>

)


查看完整回答
反對(duì) 回復(fù) 2023-03-03
  • 1 回答
  • 0 關(guān)注
  • 103 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)