Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How I can use refs to change styling class in ReactJS?

Tags:

I'm trying to change background of a div when color value changes. Here is my function which receives color value:

ChangeColor(oColor) {     this.props.ChangeColor(oColor);     console.log("Refs: ", this.refs.colorPicker.className);   }, 

Here is css class

.colorPicker {     padding-right: 25px;     background: #000;     display: inline;     margin-right: 5px;   } 

and here is my div element whose background needs to update on run-time.

<div ref='colorPicker' className={this.GetClass("colorPicker")}  /> 

I'm not sure about refs synatx so please help to fix this issue. Thanks.

like image 681
Muhammad Ateeq Azam Avatar asked Sep 20 '16 15:09

Muhammad Ateeq Azam


People also ask

How refs work in React?

Refs are a function provided by React to access the DOM element and the React element that you might have created on your own. They are used in cases where we want to change the value of a child component, without making use of props and all.


1 Answers

I found it. Here is answer:

  ChangeColor(oColor) {     this.props.ChangeColor(oColor);     this.refs.colorPicker.style.background = oColor; //this is how background will be updated    }, 
like image 123
Muhammad Ateeq Azam Avatar answered Oct 08 '22 00:10

Muhammad Ateeq Azam