首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将地图数据与顺风栅格反应

将地图数据与顺风栅格反应
EN

Stack Overflow用户
提问于 2022-03-30 14:35:41
回答 1查看 768关注 0票数 0

我希望使用顺风css将具有属性eligible: true的用户呈现到网格中。

代码语言:javascript
复制
  const users = [
    {
      name: 'John',
      age: 23,
      pic: 'someUrl'
      eligible: true
    },
    {
      name: 'Marie',
      age: 6,
      pic: 'someUrl'
      eligible: false
    },
    {
      name: 'David',
      age: 30,
      pic: 'someUrl'
      eligible: true
    },
    {
      name: 'Sarah',
      age: 20,
      pic: 'someUrl'
      eligible: true
    }
  ]
代码语言:javascript
复制
     <ul className="grid grid-rows-3 grid-flow-col gap-4 mt-4 px-10">
        {users.map((user) =>
          user.eligible ? (
            <>
              <li className="shadow-lg bg-green-100 text-green-500 text-lg font-bold text-center p-10 rounded-lg row-span-3 col-span-1">
                {user.name}
              <img src={user.pic} />
              </li>
              <li className="shadow-lg bg-green-100 text-green-500 text-lg font-bold text-center p-10 rounded-lg row-span-2">
                {user.name}
              <img src={user.pic} />
              </li>
              <li className="shadow-lg bg-green-100 text-green-500 text-lg font-bold text-center p-10 rounded-lg">
                {user.name}
              <img src={user.pic} />
              </li>
            </>
          ) : (
            ''
          )
        )}
      </ul>

使用上面的代码,为单个用户创建了三个网格,总共创建了9个网格,而不是3个网格。如果有人能给我个提示,我会很感激的。我正试图实现一个与这个我脑海中的网格完全相同的网格

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-03-30 23:30:49

我不确定是否有更好的方法来做这件事,但下面是我的尝试:

APP.js

代码语言:javascript
复制
import React from "react";
import TestS from "./components/TestS";
function App() {
  return (
    <div className="relative w-full  ">
      <TestS />
    </div>
  );
}

export default App;

TestS.jsx

如果有什么不清楚的地方,请阅读评论。

代码语言:javascript
复制
import React, { useState } from "react";

function TestS() {
  // this variable will be used inside (map) later to decide which style will be rendered
  let [count] = useState(0);
  const users = [
    {
      name: "John",
      age: 23,
      pic: "https://picsum.photos/200",
      eligible: true
    },
    {
      name: "Marie",
      age: 6,
      pic: "https://picsum.photos/200",
      eligible: false
    },
    {
      name: "David",
      age: 30,
      pic: "https://picsum.photos/200",
      eligible: true
    },
    {
      name: "Sarah",
      age: 20,
      pic: "https://picsum.photos/200",
      eligible: true
    }
  ];

  return (
    <div className="  ">
      <ul className="grid grid-rows-3 grid-flow-col gap-4 mt-4 px-10">
        {users.map((user, id) => {
          // eslint-disable-next-line
          if (user.eligible) {
            // Once there is an object passed the (if) statment the count will increase by 1 first
            count = count + 1;
            return (
              // Now it's clear that the returned element with the desired values
              // will be rendered with the classes that you specify
              count === 1 ? (
                <li
                  key={id}
                  className="shadow-lg bg-green-100 text-green-500 text-lg font-bold text-center p-10 rounded-lg row-span-3 col-span-1"
                >
                  {user.name}
                  <img
                    alt="person pic"
                    src={user.pic}
                    className="w-24 h-24 object-fill"
                  />
                </li>
              ) : count === 2 ? (
                <li
                  key={id}
                  className="shadow-lg bg-green-100 text-green-500 text-lg font-bold text-center p-10 rounded-lg"
                >
                  {user.name}
                  <img
                    alt="person pic"
                    src={user.pic}
                    className="w-24 h-24 object-fill"
                  />
                </li>
              ) : count === 3 ? (
                <li
                  key={id}
                  className="shadow-lg bg-green-100 text-green-500 text-lg font-bold text-center p-10 rounded-lg row-span-2"
                >
                  {user.name}
                  <img
                    alt="person pic"
                    src={user.pic}
                    className="w-24 h-24 object-fill"
                  />
                </li>
              ) : (
                <h1>somthing wentt wrong</h1>
              )
            );
          }
          // eslint-disable-next-line
        })}
      </ul>
    </div>
  );
}

export default TestS;

查看代码框上的活着

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71679261

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档