亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

React 多個孩子發送相同的道具

React 多個孩子發送相同的道具

慕無忌1623718 2023-03-03 13:09:08
在我的 React 應用程序中,我從 api 獲取數據并在主頁中列出所有帖子,因此我的目的是創建一個函數來為帖子評分。因此,我正在映射所有帖子并將帖子信息傳遞給Rate組件,并采取ratePost行動對帖子進行評分。行動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            })        })}但是問題是,當我發送操作時,它總是發送相同的內容postId,并且在控制臺記錄道具后,props即使我對不同的帖子進行評分,它也顯示相同。在 React DevTools Components 部分,它顯示了多個Rate組件,它們的道具與預期的不同。(我刪除了代碼的不相關部分)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>    )    }費率.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貢獻1831條經驗 獲得超9個贊

經過長時間的研究,問題出id在inputs. 它們需要是獨一無二的,所以我像那樣更新了我的代碼=>


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>

)


查看完整回答
反對 回復 2023-03-03
  • 1 回答
  • 0 關注
  • 105 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號