为了账号安全,请及时绑定邮箱和手机立即绑定

如何在反应中的兄弟姐妹之间切换

如何在反应中的兄弟姐妹之间切换

莫回无 2023-06-15 16:44:12
考虑我有一个这样的 App 组件:import React from "react";import Component from "./component";function App() {    const array = [        { key : 1 } , { key : 2 } , { key : 3 } , { key : 4 }    ]  return (    <div>        {array.map( (item) => {            <Component key={item.key} />        })}    </div>  );}export default App;组件是:import React , { useState } from "react";function Component() {    const [ style , setStyle ]= useState({        height:"50px",width:"50px",backgroundColor:"blue"    });  return (    <div style={style} onclick={} >        Content    </div>  );}export default Component;这将创建一个 App div,其中将有四个子 div 元素。我想要的是; 每当我单击其中一个内部 div 时,其余三个 div 必须将其颜色更改为红色。不是一次,而是每次我单击四个中的任何一个时。
查看完整描述

3 回答

?
一只斗牛犬

TA贡献1784条经验 获得超2个赞

添加一个状态来存储点击的(或者说,当前选择的)div


import React, { useState } from "react";

import Component from "./component";


function App() {

    const [selectedDiv, setSelectedDiv] = useState(-1);


    const array = [

        { key : 1 } , { key : 2 } , { key : 3 } , { key : 4 }

    ]

  return (

    <div>

        {array.map( (item) => {

            <Component key={item.key} clickHandler={() => {setSelectedDiv(item.key)}} isColoured={(selectedDiv === item.key || selectedDiv < 0) ? false : true} />

        })}

    </div>

  );

}


export default App;

现在Component,检查isColoured道具,如果是true,应用颜色,否则不要。


import React from "react";


function Component(props) {

  return (

    <div onClick={props.clickHandler} style={props.isColoured ? {height:"50px",width:"50px",backgroundColor:"red"} : null}>

        Content

    </div>

  );

}


export default Component;


查看完整回答
反对 回复 2023-06-15
?
FFIVE

TA贡献1797条经验 获得超6个赞

试试这个方法,


跟踪状态中选定的 div(使用 id)并Component根据状态中选定的 div 更改颜色。


import React, { useState } from "react";

import "./styles.css";


export default function App() {

  const [selectedId, setSelectedId] = useState(null);

  const array = [{ key: 1 }, { key: 2 }, { key: 3 }, { key: 4 }];

  return (

    <div>

      {array.map((item) => {

        return (

          <Component

            key={item.key}

            id={item.key}

            selectedPanel={selectedId === item.key || selectedId === null}

            onClick={() => setSelectedId(item.key)}

          />

        );

      })}

    </div>

  );

}


function Component({ id, onClick, selectedPanel }) {

  return (

    <div

      className="panel"

      style={{ backgroundColor: selectedPanel ? "blue" : "red" }}

      onClick={onClick}

    >

      Content - {id}

    </div>

  );

}

工作代码 - https://codesandbox.io/s/zealous-clarke-r3fmf?file=/src/App.js:0-770

希望这是您正在寻找的用例。如果您遇到任何问题,请告诉我。


查看完整回答
反对 回复 2023-06-15
?
ITMISS

TA贡献1871条经验 获得超8个赞

你可以添加状态


  const [selectedId, setSelectedId] = useState(null);

然后制作一个函数来呈现在这种情况下的指南


const renderGuide = ({ item, index }) => {

    console.log(item)

    const backgroundColor = item.id === selectedId ? "#FFFFFF" : "#FFFFFF";

    return (

      <Guide

        item={item}

        index={index}

        onPress={() => setSelectedId(item.id)}

        style={{ backgroundColor }}

      />

    );

  };

这样你就可以访问由 id 选择的项目


查看完整回答
反对 回复 2023-06-15
  • 3 回答
  • 0 关注
  • 95 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信