创建React App中JSON提取的对象列表
我对如何解决这个问题有些困惑。我正在尝试从DB中获取特定食谱的成分数据,然后在我的React应用中将其显示在成分组件中。
并且使用以下内容获得标题和blub blubs blub for以下内容非常容易:
我使用fetch
调用我的后端来获取数据, 我认为最好是通过对象完成)我迷路了。只是希望解决这个问题。这是JSON响应:
0:
id: 11
ingredient_id: 11
ingredient_name: "lemon"
measure: "cups"
quantity: "2"
recipe_blurb: "Delicious"
recipe_id: 6
recipe_name: "Bolognese"
[[Prototype]]: Object
1:
id: 12
ingredient_id: 12
ingredient_name: "garlic"
measure: "cloves"
quantity: "5"
recipe_blurb: "Delicious"
recipe_id: 6
recipe_name: "Bolognese"
[[Prototype]]: Object
2:
id: 13
ingredient_id: 13
ingredient_name: "butter"
measure: "g"
quantity: "500"
recipe_blurb: "Delicious"
recipe_id: 6
recipe_name: "Bolognese"
[[Prototype]]: Object
length: 3
[[Prototype]]: Array(0)
这是React代码:
export default function GetRecipe() {
const [name, setName] = useState('')
const [recipeInfo, setRecipeInfo] = useState([])
//fetch
const retrieveRecipe = function (e) {
e.preventDefault()
console.log(name)
fetch(`http://localhost:3001/getjoinedrecipes/?name=${name}`, {
method: 'GET',
headers: { 'Content-type': 'application/json' },
})
.then((resp) => resp.json())
.then((json) => {
let result = json
setRecipeInfo(result)
})
}
return (
<div>
[...]
<div>
<PageContainer
recipeName={recipeInfo.length ? recipeInfo[0].recipe_name : ''}
recipeBlurb={recipeInfo.length ? recipeInfo[0].recipe_blurb : ''}
//insert method here
></PageContainer>
</div>
</div>
)
}
另外,如果它有用 - 这是ingredientNamequant
组件,其中成分最终将最终出现 - 不确定如何动态地为每种成分创建其他成分的其他元素食谱...
import './IngredientNameQuant.css'
function IngredientNameQuant() {
return (
<div>
<div className="ingredient-quantity"></div>
<div className="ingredient-measure"></div>
<div className="ingredient-name"></div>
</div>
)
}
export default IngredientNameQuant
I'm a bit stumped about how to go about this problem. I am trying to get data for ingredients for a specific recipe from my db and then display them in an ingredients component in my React app.
I get the data using a fetch
call to my backend and it is easy enough to get the title and blurb for recipe using the following:
But when it comes to putting together the ingredient/quant/measure (which I assume would best be done via objects) I am lost. Just hoping for some assistance with this problem. Here is the JSON response:
0:
id: 11
ingredient_id: 11
ingredient_name: "lemon"
measure: "cups"
quantity: "2"
recipe_blurb: "Delicious"
recipe_id: 6
recipe_name: "Bolognese"
[[Prototype]]: Object
1:
id: 12
ingredient_id: 12
ingredient_name: "garlic"
measure: "cloves"
quantity: "5"
recipe_blurb: "Delicious"
recipe_id: 6
recipe_name: "Bolognese"
[[Prototype]]: Object
2:
id: 13
ingredient_id: 13
ingredient_name: "butter"
measure: "g"
quantity: "500"
recipe_blurb: "Delicious"
recipe_id: 6
recipe_name: "Bolognese"
[[Prototype]]: Object
length: 3
[[Prototype]]: Array(0)
Here is the React code:
export default function GetRecipe() {
const [name, setName] = useState('')
const [recipeInfo, setRecipeInfo] = useState([])
//fetch
const retrieveRecipe = function (e) {
e.preventDefault()
console.log(name)
fetch(`http://localhost:3001/getjoinedrecipes/?name=${name}`, {
method: 'GET',
headers: { 'Content-type': 'application/json' },
})
.then((resp) => resp.json())
.then((json) => {
let result = json
setRecipeInfo(result)
})
}
return (
<div>
[...]
<div>
<PageContainer
recipeName={recipeInfo.length ? recipeInfo[0].recipe_name : ''}
recipeBlurb={recipeInfo.length ? recipeInfo[0].recipe_blurb : ''}
//insert method here
></PageContainer>
</div>
</div>
)
}
Also if it is helpful - here is the ingredientnamequant
component where the ingredients will end up - not sure how to dynamically create additional elements for each ingredient in the recipe...
import './IngredientNameQuant.css'
function IngredientNameQuant() {
return (
<div>
<div className="ingredient-quantity"></div>
<div className="ingredient-measure"></div>
<div className="ingredient-name"></div>
</div>
)
}
export default IngredientNameQuant
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是我所做的,似乎有效:
Here's what I did, seems to work: