创建在React.js中的“点赞”按钮。

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

Make Like button in react.js

问题

Sure, here is the translated code after the modification:

我想要在单击按钮时只更改所选按钮而不是同时更改所有按钮

我创建了这个函数其中包含一个布尔状态并切换它

const [like, setLike] = useState(false);

const handleLike = () => {
  setLike(!like);
  console.log(like);
}

并在地图中调用它

return (
  <Grid container spacing={1} px="4%" width='100%'>
    {CardData.map((e, idx) => (
      <Box
        sx={{
          display: "flex",
          flexDirection: "column",
          alignItems: "center",
          padding: 0,
          border: "1px solid #e0e0e07a",
          position: "relative",
          borderRadius: "1.5rem",
          width: "94%",
          boxShadow: "5px 5px 46px -46px #000000",
        }}
        mb={5}
        key={idx}
      >
        <Box width='100%'>
          <Box position="absolute" top=".4rem" right=".8rem">
            <IconButton
              aria-label="fingerprint"
              color="default"
              sx={{
                zIndex: "4",
                bgcolor: "#4b4d4eb2",
                width: "2rem",
                padding: "4px",
              }}
              onClick={(e) => handleLike(e)}
            >
              {like ? <FavoriteIcon sx={{ width: ".8em", color: "#fff" }} /> : <FavoriteBorderIcon sx={{ width: ".8em", color: "#fff" }} />}
            </IconButton>
          </Box>
        </Box>
      </Box>
    ))}
  </Grid>
);

Please note that I've translated the comments and variable names while keeping the code structure intact. If you have any further modifications or questions, feel free to ask.

英文:

I wan't to make the only selected button change not all in the same time when i clicke on it

I've created this function which contains a Boolean state and the toggle it

 const [like, setLike] = useState(false);
const handleLike=()=&gt;{
setLike(!like)
console.log(like);
}

and called it here iside the map

 return (
&lt;Grid container spacing={1} px=&quot;4%&quot; width=&#39;100%&#39; &gt;
{CardData.map((e, idx) =&gt; (
&lt;Box
sx={{
display: &quot;flex&quot;,
flexDirection: &quot;column&quot;,
alignItems: &quot;center&quot;,
padding: 0,
border: &quot;1px solid #e0e0e07a&quot;,
position: &quot;relative&quot;,
borderRadius: &quot;1.5rem&quot;,
width: &quot;94%&quot;,
boxShadow: &quot;5px 5px 46px -46px #000000&quot;,
}}
mb={5}
key={idx}
&gt;
&lt;Box width=&#39;100%&#39; &gt;
&lt;Box position=&quot;absolute&quot; top=&quot;.4rem&quot; right=&quot;.8rem&quot;&gt;
&lt;IconButton
aria-label=&quot;fingerprint&quot;
color=&quot;default&quot;
sx={{
zIndex: &quot;4&quot;,
bgcolor: &quot;#4b4d4eb2&quot;,
width: &quot;2rem&quot;,
padding: &quot;4px&quot;,
}}
onClick={(e)=&gt;handleLike(e)}
&gt;
{like?&lt;FavoriteIcon sx={{ width: &quot;.8em&quot;, color: &quot;#fff&quot; }} /&gt;: &lt;FavoriteBorderIcon sx={{ width: &quot;.8em&quot;, color: &quot;#fff&quot; }} /&gt;}
&lt;/IconButton&gt;
&lt;/Box&gt;
&lt;/Box&gt;
&lt;/Box&gt;
&lt;/Grid&gt;

答案1

得分: 1

首先,将你的like状态改为一个喜欢数组:

 const [likes, setLikes] = useState([]);
 
  const handleLike=(idx)=>{
    const newLikes = [...likes];
    newLikes[idx] = !newLikes[idx];
    setLikes(newLikes);
  }

然后,确保在数组中操作正确的项目:

 return (
    <Grid container spacing={1} px="4%" width='100%' >
      {CardData.map((e, idx) => (
          <Box
            sx={{
              display: "flex",
              flexDirection: "column",
              alignItems: "center",
              padding: 0,
              border: "1px solid #e0e0e07a",
              position: "relative",
              borderRadius: "1.5rem",
              width: "94%",
              boxShadow: "5px 5px 46px -46px #000000",
            }}
            mb={5}
            key={idx}
          >
            <Box width='100%' >
              
              <Box position="absolute" top=".4rem" right=".8rem">
                <IconButton
                  aria-label="fingerprint"
                  color="default"
                  sx={{
                    zIndex: "4",
                    bgcolor: "#4b4d4eb2",
                    width: "2rem",
                    padding: "4px",
                  }}
                  onClick={(e) => handleLike(idx)}
                >
                {likes[idx] ? <FavoriteIcon sx={{ width: ".8em", color: "#fff" }} />: <FavoriteBorderIcon sx={{ width: ".8em", color: "#fff" }} />}
                  
                  
                </IconButton>
              </Box>
              </Box>
            </Box>
</Grid>

重要的是,这仅在数组中的元素位置保持一致时才有效。如果它们可能会被移动,请使用其他唯一键来识别正确的项目以进行喜欢。

英文:

First, make your like state into an array of likes:

 const [likes, setLikes] = useState([]);
 
  const handleLike=(idx)=&gt;{
    const newLikes = [...likes];
    newLikes[idx] = !newLikes[idx];
    setLikes(newLikes);
  }

Then, make sure to operate on the correct item in the array:

 return (
    &lt;Grid container spacing={1} px=&quot;4%&quot; width=&#39;100%&#39; &gt;
      {CardData.map((e, idx) =&gt; (
          &lt;Box
            sx={{
              display: &quot;flex&quot;,
              flexDirection: &quot;column&quot;,
              alignItems: &quot;center&quot;,
              padding: 0,
              border: &quot;1px solid #e0e0e07a&quot;,
              position: &quot;relative&quot;,
              borderRadius: &quot;1.5rem&quot;,
              width: &quot;94%&quot;,
              boxShadow: &quot;5px 5px 46px -46px #000000&quot;,
            }}
            mb={5}
            key={idx}
          &gt;
            &lt;Box width=&#39;100%&#39; &gt;
              
              &lt;Box position=&quot;absolute&quot; top=&quot;.4rem&quot; right=&quot;.8rem&quot;&gt;
                &lt;IconButton
                  aria-label=&quot;fingerprint&quot;
                  color=&quot;default&quot;
                  sx={{
                    zIndex: &quot;4&quot;,
                    bgcolor: &quot;#4b4d4eb2&quot;,
                    width: &quot;2rem&quot;,
                    padding: &quot;4px&quot;,
                  }}
                  onClick={(e) =&gt; handleLike(idx)}
                &gt;
                {likes[idx] ? &lt;FavoriteIcon sx={{ width: &quot;.8em&quot;, color: &quot;#fff&quot; }} /&gt;: &lt;FavoriteBorderIcon sx={{ width: &quot;.8em&quot;, color: &quot;#fff&quot; }} /&gt;}
                  
                  
                &lt;/IconButton&gt;
              &lt;/Box&gt;
              &lt;/Box&gt;
            &lt;/Box&gt;
&lt;/Grid&gt;

Importantly, this will only work if elements are in consistent positions in the array. If they could get moved around, then use some other unique key to identify the correct item to like.

答案2

得分: 0

你应该为每个 Box 创建一个类似于 useState 的功能。你可以这样做:

const Child = (props) => {
    const [like, setLike] = useState(false);
     
    const handleLike = () => {
       setLike(!like)
       console.log(like);
     };

    return (
        /* 在 Box 内部渲染的内容 */
    )
};

const Parent = () => {
    return (
        {CardData.map((e, idx) => (
            <Child key={idx} props={props}>
        )}
    )
}
英文:

You should create a like useState for each Box. You can do something like this:

const Child = (props) =&gt; {
    const [like, setLike] = useState(false);
     
    const handleLike= () =&gt; {
       setLike(!like)
       console.log(like);
     };

    return (
        /* What renders inside the Box */
    )
};

const Parent = () =&gt; {
    return (
        {CardData.map((e, idx) =&gt; (
            &lt;Child key={idx} props={props}&gt;
        )}
    )
}

huangapple
  • 本文由 发表于 2023年7月17日 09:35:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76701051.html
匿名

发表评论

匿名网友

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

确定