我创建了多个组件,它们具有相同的层次结构,在它们内部我也调用其他组件调用,现在我想在我的上创建函数,它将更新我作为道具传递给我的组件的值。我设法将函数作为道具传递,但无法将值从孩子作为参数传递给函数,因此我只能更新特定于该孩子的道具。
应用程序
function App() {
// eslint-disable-next-line
const [content, setContent] = useState(images)
const [count, setCount] = useState(content.votes)
console.log(count)
const upVote = (id) => {
alert('up =>', id)
}
const downVote = () => {
alert('down')
}
return (
<div className="App">
<div className="grid">
{content.map((picture, index) => {
return <Card key={index} picture={picture} teste={[upVote, downVote]}/>
})
}
</div>
</div>
)
}
卡片
function Card({ picture, teste }) {
return (
<div className="card">
<div className="container">
<img
width="300"
alt={`id: ${picture.id}`}
src={picture.src}
className="image"
/>
<Options votes={0} originalPost={picture.original_post} teste={teste[0]}/>
</div>
</div>
)
}
慕莱坞森
跃然一笑
随时随地看视频慕课网APP
相关分类