I have a js file handling my css where I am trying to change the value of an object, but the value stays the same.
JavaScript
x
19
19
1
let inputBoxInner = {
2
width: "80%",
3
marginTop: 5,
4
alignItems: "center",
5
color: "#397185",
6
cursor: "text",
7
height: 36,
8
border: "1px solid #80cfc6",
9
visibility: "visible",
10
opacity: 0.2,
11
setOpacity: function (e) {
12
this.opacity = e
13
};
14
};
15
16
module.exports = {
17
inputBoxInner
18
};
19
JavaScript
1
21
21
1
import React, {Component} from "react";
2
import {inputBoxInner} from "../css/style.js";
3
4
export default class Input extends Component {
5
state = {
6
borderOpacity: 1,
7
id: ""
8
};
9
10
return(
11
<div
12
className="input"
13
onClick={(e) => {
14
inputBoxInner.setOpacity(this.state.borderOpacity);
15
this.setState({id: e.target.className});
16
}}
17
style={inputBoxInner}
18
/>
19
);
20
};
21
I assume the “this.opacity” is only returning a reference and not modifying the actual object and I am unsure of how to make this object mutable.
How would I go about changing this value?
Advertisement
Answer
You should save a clicked state in the state and set opacity depending on it.
JavaScript
1
15
15
1
state = {
2
borderOpacity: 1,
3
id: "",
4
isClicked: false
5
};
6
7
return(
8
<div
9
className="input"
10
onClick={(e) => { this.setState({id: e.target.className, isClicked: true }); }}
11
style={{inputBoxInner, opacity: this.state.isClicked ?
12
this.state.borderOpacity : inputBoxInner.opacity}}
13
/>
14
);
15