简体   繁体   English

反应单个可选组件

[英]React single selectable component

I'm new to react and I'm trying to make a selection button where only a single item can be selected and it deselects the other items.我是新来的反应,我正在尝试制作一个选择按钮,其中只能选择一个项目并取消选择其他项目。 I have made it as far as selecting an item, but not the deselecting the other items part.我已经做到了选择一个项目,但没有取消选择其他项目部分。

Below is my component code:下面是我的组件代码:

class Diamond extends React.Component {
  constructor(props) {
    super(props);
    this.selectDiamond = this.selectDiamond.bind(this);
    this.state = {selected : false};
  }

  selectDiamond() {
    this.setState( {selected : true} );
  }

  render() {
    if (this.state.selected == true) {
      return (
        <div onClick={this.selectDiamond} className="diamond-selected hover:scale-[105%] duration-300 ">{this.props.text}</div>
      )
    } else {
      return(
        <div onClick={this.selectDiamond} className="diamond-unselected hover:scale-[105%] duration-300 ">{this.props.text}</div>
      )
    }
  }
}

and this is how I render it:这就是我渲染它的方式:

<div>
    {["BOY", "GIRL", "BAKLA", "TOMBOY"].map((item, i) => (
        <Diamond key={i} text={item}/>
        )
    )}
</div>

I know I need a reference to the <Diamond/> s so I tried something like:我知道我需要对<Diamond/>的引用,所以我尝试了类似的方法:

let diamonds = [];

//and on the constructor method of the component 
constructor(props) {
   super(props);
   diamonds.push(this);
}

but for some reason when I call diamonds.length it gives me double the number of the created items, and I also do not know what to do from there.但出于某种原因,当我调用diamonds.length时,它使我创建的项目数量增加了一倍,而且我也不知道从那里该做什么。 I also would like the first item to be selected by default.我还希望默认选择第一项。

I would reccomend using hooks instead of class components.我建议使用钩子而不是 class 组件。 Here is snippet how it can be done:这是如何完成的片段:

const Diamond = ({ text, setSelected, isSelected }) => {
  return (
    <div
      onClick={setSelected}
      className={`diamond-${
        isSelected ? "" : "un"
      }selected hover:scale-[105%] duration-300`}
    >
      {text}
    </div>
  );
};

const ListDiamonds = () => {
  const [selectedIdx, setSelectedIdx] = useState(null);

  return (
    <div>
      {["BOY", "GIRL", "BAKLA", "TOMBOY"].map((item, i) => (
        <Diamond
          key={i}
          text={item}
          isSelected={selectedIdx === i}
          setSelected={() => setSelectedIdx(i)}
        />
      ))}
    </div>
  );
};

I did not test it but in general concept it like that.我没有测试它,但在一般概念上它是这样的。

Also do not push this from constructor, it is bad practice.也不要从构造函数中推送this ,这是不好的做法。 You could read rect lifting state up: https://reactjs.org/docs/lifting-state-up.html or better with hooks: https://www.freecodecamp.org/news/what-is-lifting-state-up-in-react/你可以阅读 rect lifting state up: https://reactjs.org/docs/lifting-state-up.html或更好的 hooks: https://www.freecodecamp.org/news/what-is-lifting-state-上反应/

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

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