简体   繁体   English

如何通过 React 中的对象数组 .map()

[英]How to .map() through an array of objects in React

I'm trying to map through some data that looks like this:我正在尝试映射一些看起来像这样的数据:

[
{
  "id": "cambridgeshire",
  "name": "Cambridgeshire"
},
{
  "id": "bedfordshire",
  "name": "Bedfordshire"
}
]

And display it as a list.并将其显示为列表。

I'm getting the error of allAreas is undefined.我收到 allAreas 的错误未定义。 I have a feeling its because of the JSON Object which I'm unfamiliar with.我有一种感觉是因为我不熟悉 JSON 对象。

On checking the state of allAreas it shows undefined.在检查 allAreas 的状态时,它显示未定义。

Heres the code Ive tried这是我试过的代码

const Directory = () => {
  const [allAreas, setAllAreas] = useState([]);

  useEffect(() => {
    fetchAllAreasData().then((data) => setAllAreas(data));
  }, []);
  return (
    <div className='directory'>
      <ul>
        {allAreas.map((area) => (
          <li key={area.id}>{area.name}</li>
        ))}
      </ul>
    </div>
  );
};

To clarify this is an array of objects澄清这是一个对象数组

Make sure the allArea exists before mapping.在映射之前确保 allArea 存在。

const Directory = () => {
  const [allAreas, setAllAreas] = useState([]);

  useEffect(() => {
    fetchAllAreasData().then((data) => setAllAreas(data));
  }, []);
  return (
    <div className='directory'>
      <ul>
        {allAreas && allAreas.map((area) => (
          <li key={area.id}>{area.name}</li>
        ))}
      </ul>
    </div>
  );
};

If your problem didn't fix, use the useReducer instead because of some react problems in useState with arrays in sometimes.something like this:如果您的问题没有解决,请改用 useReducer,因为 useState 中的一些反应问题与有时中的数组有关。像这样:

function reducer(state = [], action) {
 return action;
}

const Directory = () => {
  const [allAreas, dispatch] = useReducer(reducer, []);

  useEffect(() => {
    fetchAllAreasData().then((data) => dispatch(data));
  }, []);
  return (
    <div className='directory'>
      <ul>
        {allAreas.length > 0 && allAreas.map((area) => (
          <li key={area.id}>{area.name}</li>
        ))}
      </ul>
    </div>
  );
}; 
const [allAreas, setAllAreas] = useState([]); // your defaulted state must be [] instead of {}

只需将函数的第一行更改为:

const [allAreas, setAllAreas] = useState([]);

It may be issue because your map function gets called before promise resolved and you get data.这可能是问题,因为您的 map 函数在承诺解决之前被调用并且您获得数据。

To avoid this please check the length of 'allAreas' before iterating over it.为避免这种情况,请在迭代之前检查'allAreas'的长度。

Also the intitial values assigned to 'allAreas' object instead of array.还有分配给'allAreas'对象而不是数组的初始值。 Change initial assignment to an array.将初始分配更改为数组。

const Directory = () => {
  const [allAreas, setAllAreas] = useState([]);

  useEffect(() => {
    fetchAllAreasData().then((data) => setAllAreas(data));
  }, []);
  return (
    <div className='directory'>
      <ul>
        {allAreas.lenght && allAreas.map((area) => (
          <li key={area.id}>{area.name}</li>
        ))}
      </ul>
    </div>
  );
};

try,尝试,

 <ul>
     {allAreas && allAreas.map((area) => (
         <li key={area.id}>{area.name}</li>
     ))}
 </ul>

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

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