0

I am rendering a div multiple times by using map javascript function in my react application. I want to set css (width) of each element. Width will be element specific. I am rendering div like this

    {this.props.face_clicked_reducer.person_timeline.map((item, i) => (
             <div className= {`marker-item-${i}`} id="sele1"></div>
          ))
    }

className of each div is different. How can I set css of each element. For setting width I will be needing {item.time} reference.

I tried setting it in componentDidMount like this

this.props.face_clicked_reducer.person_timeline.map((item, i) => (
            $("<div className= {`marker-item-${i}`}></div>").css({
                "width": ((item.endTime - item.startTime) * (100/this.props.player_time_update_reducer.video_duration)) + '%',
                "left": ((item.endTime) * (100/this.props.player_time_update_reducer.video_duration)) + '%'
            })

    )).css('backgorund', 'red')

But it didn't work. I am not getting any errors in console.

7
  • 1
    Tip: Please avoid using jquery with React. React is unaware of changes made to the DOM outside of React. It determines updates based on its own internal representation, and if the same DOM nodes are manipulated by another library, React gets confused and has no way to recover. There are ways, so that you can use other plugins(like jqeury) but I personally don't like it or recommend it. Commented Sep 5, 2017 at 10:37
  • @RaghavGarg you meant "avoid" I think. Commented Sep 5, 2017 at 10:38
  • I agree, But a lot of libraries are just in jquery and to make webpage look good I end up using jquery. Commented Sep 5, 2017 at 10:40
  • @bennygenel, Thanks for noticing. Commented Sep 5, 2017 at 10:40
  • @RaghavGarg, while you cannot use jQuery in the fashion demonstrated in OP's code (because it returns a DOM element, not a ReactComponent), there are many valid use-cases of jQuery with React. Such as animations or inline-styling. Even certain DOM manipulations can be done without having "smelly code". Commented Sep 5, 2017 at 11:06

2 Answers 2

1

There are a few issues with your code:

  1. You are using the same id for each item in the array. IDs need to be unique.
  2. The idea of css classes are to select multiple similar elements and not to have a unique class for each element.
  3. If you re-arrange the items in the array, the css-class for the moved items will change.

That said, you could do apply an inline-styling instead:

this.props.face_clicked_reducer.person_timeline.map(
  (item, i) => <div style={{width: (50 * i) + "%", height:(20 * item.time) + "%"}}></div>
);

Alternatively:

this.props.face_clicked_reducer.person_timeline.map(
  (item, i) => {
    let s = {
      width: (50 * i) + "%",
      height: (20 * item.time) + "%"
    };
    return <div style={s}></div>;
  }
);

This prevents you from defining person_timeline.length number of classes in your css.

Sign up to request clarification or add additional context in comments.

Comments

0

You can use style prop for styling components.

{
    this.props.face_clicked_reducer.person_timeline.map((item, i) => {
        const style = {
            width: ((window.innerWidth * 0.2) * i),
            height: ((window.innerHeight * 0.2) * item.time)
        }
        return (<div className={`marker-item-${i}`} style={style}></div>)
    })
}

2 Comments

I want values in percent. I am not sure if style attribute can set dimentions in percent.
You can use window.innerWidth and window.innerHeight and calculate the desired percentage I think.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.