哈士奇WWW
2023-05-18 10:10:43
我有這條調(diào)用PriceWithId函數(shù)的路由<Route path='/collections/:pId' component={PriceWithID} />功能是這樣的const PriceWithID = ({match}) => { let test = Object.entries(match); let test2 = test.filter(([prop])=>(prop === 'params')); let test3 = test2[0][1]; let test4 = Object.values(test3)[0] return( <Price image={IMGS.filter((i)=>(i.id === parseInt(test4,10)))[0]}/> ); }我知道有一個(gè)更好的 oneline 方式來(lái)實(shí)現(xiàn)test4。我使用的方法伴隨著反復(fù)試驗(yàn)。誰(shuí)能告訴我更好的方法,以便我可以用它替換 parseInt 中的 test4
2 回答

慕妹3146593
TA貢獻(xiàn)1820條經(jīng)驗(yàn) 獲得超9個(gè)贊
使用this.props.match.params.pId
或功能組件
const PriceWithID = (props) =>
{
let pId = props.match.params.pId
return(
<Price image={IMGS.filter((i)=>(i.id === parseInt(pId))[0]}/>
);
}

慕田峪4524236
TA貢獻(xiàn)1875條經(jīng)驗(yàn) 獲得超5個(gè)贊
可以通過(guò)這種方式改進(jìn)以前的答案:
const PriceWithID = ({match}) =>
{
return(
<Price image={IMGS.filter((i)=>(i.id === parseInt(match.params.pId,10)))[0]}/>
);
}
但由于之前的解決方案是正確的,我將其標(biāo)記為答案。
添加回答
舉報(bào)
0/150
提交
取消