Reputation: 5460
Understanding that this.setState()
will trigger a re-render of a component, if I have a view and I do not need to trigger a re-render of say, for example a View, is it acceptable to set a variable on this? Is there any downside to doing so since I only need to reference that variable value within the scope of the View code?
For example:
this.adHeight = 100
Versus:
this.setState({ adHeight: 100 })
Upvotes: 0
Views: 311
Reputation: 3801
You are not providing enough information to give you an exact answer so I will start guessing. If that addHeight
variable is not affecting your component UI, why do you need it in your component? Is that variable a constant or it can change?
You could do:
const addHeight = 100;
// or
const addHeight = someConstantFile.addHeight;
// or
const addHeight = someFile.someFunction();
export default MyComponent extends React.Component {
// use the addHeight somewhere
}
If that variable is not a constant, you could pass it as a prop too (but you are saying that it does not affect your render
function so I am wondering what is the purpose of that variable). Maybe you could paste the whole component here.
But anyway, I would say yes, you can do what you are asking. I'd recommend you to use _
thought, for example: this._listener
. Then you could for example access this._listener
from your componentDidMount
and from your componentWillUnmount
(typical example). BUT if you find yourself accessing that variable from your render
function, it would mean that this variable should actually be a prop or part of your component state.
Upvotes: 1