简体   繁体   English

我如何在反应中传递 map function 前面的道具?

[英]How do I pass a prop in front of map function in react?

I'm importing some json files as:我将一些 json 文件导入为:

import general from './Data/general.json';
import health from './Data/health.json';
import business from './Data/business.json';

And I am trying to iterate through an array "articles" in the json file, the names of the files are same as the category prop I'm passing.我正在尝试遍历 json 文件中的数组“文章”,文件名与我传递的类别道具相同。 So, I want to know how can I change the file from which my map function takes data using prop inputs which change on press of buttons on the navbar.所以,我想知道如何更改我的 map function 使用道具输入从中获取数据的文件,这些输入会在按下导航栏上的按钮时发生变化。

Trying this, but it doesn't work:尝试这个,但它不起作用:

{props.category.articles.map((element) => {

Doing this works but I can't change it according to the prop:这样做有效,但我无法根据道具更改它:

{general.articles.map((element) => {

I've tried enclosing props.category in { },( ), and passing them after declaring them in a const variable but none of it seems to work.我试过将 props.category 包含在 { },( ) 中,并在将它们声明为 const 变量后传递它们,但似乎都不起作用。

You can try something like this你可以尝试这样的事情

  const data = {general,health,business};

  {data[props.category].ariticles.map(...)}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM