【发布时间】:2020-10-26 04:56:06
【问题描述】:
我正在制作一个食谱 Web 应用程序,但我没有使用 Material ui 或 bootstrap 中的任何内置卡片功能。我想连续有 4 个配方组件而不是两个。食谱组件还有一个名为成分的按钮,因此每当我单击该按钮时,它应该只显示该食品食谱的成分,而是显示该行中所有食谱组件的食谱。请告诉我如何解决这个问题。 我将 github repo 链接附加到项目中:- https://github.com/ankita413/recipe 成分组件在成分.jsx 中定义,它的 css 在 recipe.css 中 配方组件在 recipe.jsx 中,它的 css 在 recipe.css 中 同样对于按钮功能,我在此处附上代码和图像[当我单击第一个配方的成分按钮时,第二个成分按钮也会打开,但我不希望显示任何内容,只有我单击的成分按钮应该打开][1] [1]:https://i.stack.imgur.com/z4DUY.png 包含配料按钮的配方组件代码
import React, { useState } from 'react';
import './styles/recipe.css';
import Recipeingredient from './ingredient';
const Recipe = ({title,calories,image,ingredients}) => {
const [show,setShow] = useState(false);
return(
<div className="recipe">
<h2>Search Recipe</h2>
<h3>{title}</h3>
<p>Calories: {Math.floor(calories)}</p>
<img src={image} alt = {title} ></img>
<button onClick={() => setShow(!show)}>Ingredients</button>
{show && <Recipeingredient ingredients = {ingredients}/>}
</div>
);
}
export default Recipe;
【问题讨论】:
-
您显示的代码无法解释为什么您甚至会得到“连续 2 个”-也许您在
recipe上设置了width- 如果是这样,请将宽度减小一半得到每行双倍的项目 -
我还建议,在 CodesandBox 中创建一个示例并在此处共享链接,此外,您在单个问题中提到了 2 个不同的问题陈述。请在此处分享第一个问题的代码。
标签: javascript css reactjs