简体   繁体   English

如何从 function 返回一个数组并将其元素用作 JSX

[英]How can I return an array from function and use it's elements as a JSX

I don't know why but for some reason I can't return an array from the function and so I can't use it as a JSX.我不知道为什么,但由于某种原因,我不能从 function 返回一个数组,所以我不能将它用作 JSX。 Can you explain me how can I do that?你能解释一下我该怎么做吗? or I would be very happy if you'll show me the solution.或者如果你能告诉我解决方案,我会很高兴。 I just need to print objects as a text from filteredArr array.我只需要将对象打印为 filtersArr 数组中的文本。 I'm new to react and looks like everything is not same in react and javascript.我是新来的反应,看起来反应和 javascript 的一切都不一样。 Here's my code:这是我的代码:

 const payloadURL = "https://swapi.dev/api/people/"; const usePeopleData = (filterParams) => { fetch(payloadURL).then((response) => response.json()).then((data) => { let result = data.results; // To put every element as an object in array let arr = []; for (let i in result) { let obj = { name: result[i].name, mass: result[i].mass, height: result[i].height, gender: result[i].gender } arr.push(obj); } let condition = (prop) => prop.height > filterParams.minHeight && prop.gender === filterParams.gender? true: false; let filteredArr = arr.filter((person) => condition(person)); console.log(filteredArr); return filteredArr; }); }; const filterPeopleParams = { gender: "male", minHeight: 130 }; function App() { usePeopleData(filterPeopleParams); return ( <div className="App"> <div> <label> Mission name: <input /> </label> </div> <h3>People</h3> <div> <p>/* NEED TEXT HERE */</p> </div> </div> ); } App();
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.4.2/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.4.2/umd/react-dom.production.min.js"></script>

Thanks in advance!提前致谢!

at first be carefull in Naming the methods(usePeopleData is detected to be a react customhook. have a look at customhooks naming), then you need to be more deep in react concepts like, declaring a var in state, and calling the api inside useEffect cyclehook and setting the data in state.首先在命名方法时要小心(usePeopleData 被检测为 react customhook。看看 customhooks 命名),然后您需要更深入地了解 react 概念,例如在 state 中声明一个 var,并在 useEffect 中调用 api Cyclehook 并在 state 中设置数据。 them showing the result in render.他们在渲染中显示结果。

check this:https://codesandbox.io/s/eloquent-cannon-2vrfs?file=/src/App.js检查这个:https://codesandbox.io/s/eloquent-cannon-2vrfs?file=/src/App.js

import "./styles.css";
import { useEffect, useState } from "react";
export default function App() {
  const [missionName, setMissionName] = useState("");
  const [list, setList] = useState([]);

  const geteopleData = (filterParams) => {
    const payloadURL = "https://swapi.dev/api/people/";
    fetch(payloadURL)
      .then((response) => response.json())
      .then((data) => {
        let result = data.results;

        // To put every element as an object in array
        let arr = [];
        for (let i in result) {
          let obj = {
            name: result[i].name,
            mass: result[i].mass,
            height: result[i].height,
            gender: result[i].gender
          };
          arr.push(obj);
        }
        let condition = (prop) =>
          prop.height > filterParams.minHeight &&
          prop.gender === filterParams.gender
            ? true
            : false;
        let filteredArr = arr.filter((person) => condition(person));
        console.log("filteredArr: ", filteredArr);
        setList(filteredArr);
        // return filteredArr;
      });
  };

  useEffect(() => {
    let filterPeopleParams = { gender: "male", minHeight: 130 };
    geteopleData(filterPeopleParams);
  }, [missionName]);

  return (
    <div className="App">
      <div className="App">
        <div>
          <label>
            Mission name:{" "}
            <input
              value={missionName}
              onChange={(e) => setMissionName(e.target.value)}
            />
          </label>
        </div>
        <h3>People</h3>
        <div>
          {list.map((item) => {
            return <p>{item.name}</p>;
          })}
        </div>
      </div>
    </div>
  );
}


````

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

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