26
Solo Week + React (pt. 1)
When we write little helper variables to help with conditional rendering or just more concise, why are those declared in the render function and not in the class component definition?
I'm starting to understand something that puzzled me in my earliest React days. So I believe React re-renders components (or parts of them) upon state change. That's why you use setState (the function) and not regular object access (dots or brackets) to change state variables. I imagine setState triggers that re-rendering.
this.setState({styles: data.results})
this.setState({selectedStyle: data.results[0]})
this.setState({stylePhotos: data.results[0].photos})
styles.selectedStyle.photos
breaks the page (more on this later) because selected style is an empty object on page load (so is styles for that matter) and you get an error for trying to access values on undefined objects.this.state = {
styles: [],
focusedImageIndex: 0,
carouselIndex: 0,
selectedStyle: {},
stylePhotos: []
};
We decided as a team that we'll use Context (which I think is a state-management library like Redux) in this project. Maybe Context will be the answer to my question above. But I wonder how much refactoring work I'm creating for myself by not starting with Context right off the bat.
I'm not deliberately expanding my React knowledge. Instead, I'm using it in the limited way that I know and assuming that if and when problems occur, that I'll be able to troubleshoot my way out of them. Is that a terrible approach? Should I do a deeper dive and find some tools that might help me later? Will I be able to remember those tools and recognize opportunities to apply them with that approach? I'm a hands-on learner, so would I need to write test/learning code as I went through the docs. And would that process eat up time that I should be applying to the current project?