r/reactjs • u/timmonsjg • Dec 03 '18
Needs Help Beginner's Thread / Easy Questions (December 2018)
Happy December! βοΈ
New month means a new thread π - November and October here.
Got questions about React or anything else in its ecosystem? Stuck making progress on your app? Ask away! Weβre a friendly bunch. No question is too simple. π€
π Want Help with your Code? π
- Improve your chances by putting a minimal example to either JSFiddle or Code Sandbox. Describe what you want it to do, and things you've tried. Don't just post big blocks of code!
- Pay it forward! Answer questions even if there is already an answer - multiple perspectives can be very helpful to beginners. Also there's no quicker way to learn than being wrong on the Internet.
Have a question regarding code / repository organization?
It's most likely answered within this tweet.
New to React?
π Here are great, free resources! π
38
Upvotes
View all comments
1
u/CoolPrase Dec 15 '18
I have a sidebar I open and close with a setState() function inside render, which I have heard is bad, because render() has to be pure(whatever that is, the best explanation I got was that it shouldn't have "side effects"). I use
let toggled = this.state.toggle ? 'block' : 'none';
let reversed = !this.state.toggle ? 'block' : 'none';
to set the display property of the sidebar and the button that opens it accordingly. Upon pressing the close button the sidebar disappears and the button opening it appears, because I have
onClick={() => this.setState({toggle: !this.state.toggle})
how do I do this correctly, without setting state inside render()?
I didn't just write here before trying to solve it, best idea I got is by creating another action just for that. Is that the way to go?
P.S. I have a much bigger problem I solve with the same technique, but I hope the answer to this question will help me solve the larger one. If not, I hope y'all will stick around to help me solve it.
Thanks in advance!