在react.js中传递属性/添加到收藏夹

时间:2019-10-23 12:02:25

标签: javascript reactjs react-props

我有一个映射的项目的手风琴,每个项目都有一个Favourite-图标。单击该图标后,它需要更改颜色(已经这样做了)并将{name.toUpperCase()}的{​​{1}}属性传递到BadgeButton页。有人可以解释/告诉我如何吗?任何帮助将不胜感激。

这是Favourites页:

Accordion

export default function Accordion({ name, description, use, recipe, liked }) { const [clicked, setClicked] = useState(false); function handleClick() { setClicked(!clicked); } return ( <AccordionDiv> <AccordionHead onClick={handleClick}> <div> <BadgeButton>{name.toUpperCase()}</BadgeButton> <StyledArrow clicked={clicked} /> </div> <div>{use}</div> </AccordionHead> <AccordionContent clicked={clicked}> <span>{description}</span> <p>{recipe}</p> <StyledIcons> <Favourite liked={liked} /> <Vote /> </StyledIcons> </AccordionContent> </AccordionDiv> ); } 页:

Favourites

1 个答案:

答案 0 :(得分:0)

您必须将状态设置为州,然后将其作为道具传递给收藏夹。像这样的东西:

const [nameClicked, setNameClicked] = useState("");

function handleClick() {
    setClicked(!clicked);
    setNameClicked(name);
}

<AccordionDiv>
    ...
    <Favourite liked={liked} name={nameClicked} />
</AccordionDiv>