TransWikia.com

adding onclick to an array of items

Stack Overflow Asked by helpmepiliizz on December 11, 2021

I have this problem where if I click on an item, all the items in that column gets affected by the click. I want only that very item where I clicked to have the class. but in my code, all the other items of that column are getting the class when I click on a certain single item. I want to make a div go fullscreen when I click on that particular div. kinda like modal pop up, here I want that the div slowly animates to fullscreen

export default class App extends React.Component {
  constructor(props) {
    super(props);
    this.addActiveClass = this.addActiveClass.bind(this);
    this.state = {
      activeIndex: -1
    };
  }
  addActiveClass(activeIndex) {
    this.setState(prev => ({
      activeIndex: prev.activeIndex === activeIndex ? -1 : activeIndex
    }));
  }

  render() {
    return (
      <div className="container">
        {Array.from({ length: 30 }).map((item, index) => {
          return (
            <div
              key={index}
              style={{
                background: randomColor(colors),
                height: randomHeight(100, 200)
              }}
              className={this.state.activeIndex === index ? "full" : ""}
              onClick={() => this.addActiveClass(index)}
            />
          );
        })}
      </div>
    );
  }
}
* {
  box-sizing: border-box;
}

body {
  margin: 40px;
  background-color: #fff;
  color: #444;
  font: 2em Sansita, sans-serif;
}

.container {
  display: flex;
  flex-direction: column;
  flex-wrap: wrap;
  max-height: 100vh;
}

.container > * {
  border: 2px solid orange;
  border-radius: 5px;
  margin: 10px;
  padding: 10px 20px;
  background-color: red;
  color: #fff;
}

.full{
  width: 100%;
  height: 100%;
  transition: 2s;
}

sandbox demonstrating the problem

5 Answers

Adding !important overrides the inline-style height.

Height is set to 100vh to expand the div into full screen.

.full{
  width: 100%;
  height: 100vh !important;
  transition: 2s;
}

Answered by Tan Joo Hong on December 11, 2021

Pass down an event (e) from your onClick, then access that specific item through e.target.className. This way only the clicked item is updated, even if dynamically rendered from an array:

handleClick = e => { e.target.classNamme = 'class' }

<div className='' onClick={e => handleClick(e)}></div>

Alternatively, if you're keeping your classes in the state, you can just replace the string value with your variable and control in inside the function, as you've done in the example you've provided.

Answered by Kay Kostadinov on December 11, 2021

The class adding logic works ok. You can observe this by checking the elements on developer tools. The problem is you are adding an inline style to your elements.

style={{
  background: randomColor(colors),
  height: randomHeight(100, 200)
}}

So, after each new render, this overrides the full class and give new height and new color.

The other problem is your full class. You are using 100% for height and width and probably this causes problems for your flex.

Just remove the inline style part and instead of using a percentage give custom width and height to full class, then you can see adding the class logic works well.

Answered by devserkan on December 11, 2021

You'll have to make sure you're adding the class to the event target. Your onClick function needs to take the event as one of it's arguments, and you need to use event.target in the function body to make sure you're only affecting the element that is clicked. Hope that makes sense!

https://developer.mozilla.org/en-US/docs/Web/API/Event/target

Answered by Ross on December 11, 2021

I don't know exactly what kind of effect you would like to achieve with the selected item but items in the column are affected because of the default value of flexbox align-items which is stretch. The other items are stretching to match the selected element. Try to add align-items: flex-start to your .container class.

Answered by tjankowski on December 11, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP