我如何通过React Map功能前的道具传递?

发布于 2025-01-21 05:17:57 字数 542 浏览 2 评论 0 原文

我正在将一些JSON文件导入:

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

我正在尝试通过JSON文件中的数组“文章”迭代,文件的名称与我通过的类别prop相同。因此,我想知道如何使用Prop Inputs更改映射功能从中获取数据的文件,这些文件输入会在Navbar上按下按钮的按下。

尝试此操作,但它不起作用:

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

执行此操作,但我无法根据道具进行更改:

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

我尝试过将props.category in {},()封闭,并在将它们声明为const变量后传递,但似乎没有一个工作。

I'm importing some json files as :

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. 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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤独岁月 2025-01-28 05:17:57

你可以尝试这样的东西

  const data = {general,health,business};

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

You can try something like this

  const data = {general,health,business};

  {data[props.category].ariticles.map(...)}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文