在React中从文件显示多个数值

huangapple go评论193阅读模式
英文:

Display multiple values from file in React

问题

我正在尝试使用React显示一个待办事项清单所以我正在加载一个看起来像这样的json文件
{
  "todos":
  [{"id":1,"todo":"为我关心的人做点好事","completed":true,"userId":26},
   {"id":2,"todo":"记住五十个州及其首府","completed":false,"userId":48},
   {"id":3,"todo":"看一部经典电影","completed":false,"userId":4}],
  "total":150,"skip":0,"limit":30
}
而且我也在使用mui的复选框所以我想显示一个复选框"completed"为true时它的"defaultChecked"应该为开启状态

我成功加载了json并显示了复选框但我无法弄清楚如何全部显示它们

代码
```javascript
import './App.css';
import React, { Component }  from 'react';
import { Checkbox, FormControlLabel, FormGroup } from '@mui/material';

import data from './todo-list.json';

function App() {
  for(let i = 0; i < data.todos.length; i++) {
    if(data.todos[i].completed) {
      var checkbox = console.log(data.todos[i].id, data.todos[i].todo);
    }
    else {
      console.log(data.todos[i].id, data.todos[i].todo);
    }
  }
  
  return (
    <div>
      <header>
        <FormGroup>
          <FormControlLabel control={<Checkbox defaultChecked />} label="Label" />
          <FormControlLabel control={<Checkbox />} label="Label 2" />
        </FormGroup>
      </header>
    </div>
  );
}

export default App;
英文:

I'm trying to display a checklist using React. So I'm loading a json file that looks something like this:
{
"todos":
[{"id":1,"todo":"Do something nice for someone I care about","completed":true,"userId":26},{"id":2,"todo":"Memorize the fifty states and their capitals","completed":false,"userId":48},{"id":3,"todo":"Watch a classic movie","completed":false,"userId":4}]
,"total":150,"skip":0,"limit":30
}
And I'm also using mui's checkboxes. So I want to display a checkbox and its "defaultChecked" should be on when "completed" is true.

I managed to load the json and display checkboxes, but I cant figure out how to display all of them.

Code:

import &#39;./App.css&#39;;
import React, { Component }  from &#39;react&#39;;
import { Checkbox, FormControlLabel, FormGroup } from &#39;@mui/material&#39;;

import data from &#39;./todo-list.json&#39;;

function App() {
  for(let i = 0; i &lt; data.todos.length; i++) {
    if(data.todos[i].completed) {
      var checkbox = console.log(data.todos[i].id, data.todos[i].todo);
    }
    else {
      console.log(data.todos[i].id, data.todos[i].todo);
    }
  }
  
  return (
    &lt;div&gt;
      &lt;header&gt;
        &lt;FormGroup&gt;
          &lt;FormControlLabel control={&lt;Checkbox defaultChecked /&gt;} label=&quot;Label&quot; /&gt;
          &lt;FormControlLabel control={&lt;Checkbox /&gt;} label=&quot;Label 2&quot; /&gt;
        &lt;/FormGroup&gt;
      &lt;/header&gt;
    &lt;/div&gt;
  );
}

export default App;

I managed to load the json and display checkboxes, but I cant figure out how to display all of them.

答案1

得分: 0

你需要映射你的待办事项并相应地呈现。

你的返回应该像这样:

return (
   <div>
     <header>
       <FormGroup>
         {data.todos.map(todo => <FormControlLabel control={<Checkbox checked={todo.completed} />} label={todo.todo} /> )}
       </FormGroup>
     </header>
   </div>
);
英文:

You need to map on your todos and render accordingly.

Your return should look like this.

 return (
    &lt;div&gt;
      &lt;header&gt;
        &lt;FormGroup&gt;
          {data.todos.map(todo =&gt; &lt;FormControlLabel control={&lt;Checkbox checked={todo.completed} /&gt;} label={todo.todo} /&gt; )}
        &lt;/FormGroup&gt;
      &lt;/header&gt;
    &lt;/div&gt;
  );

huangapple
  • 本文由 发表于 2023年3月7日 00:51:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75653612.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定