Creating Flexible Components with Props
Coding in Public did an excellent video about this which I didn’t understand at the time. Worth a watch if you are wondering how to best make these types of flexible components
You can tell that I don’t have much experience with JavaScript frameworks. It took me time to understand the power of components and using props. I will admit that I used to write too much code.
Originally, I created three components to control the alignment of images on my blog. I had a left-aligned, center-aligned, and right-aligned component. Don’t do things this way. It isn’t efficient. This can be only one component but it requires being clever with your props.
This is how my image align component looks now. Where I can determine which alignment I want by using props to set the appropriate class.
You see in my props that I have float, align, and flex. When I use my component, I can add the appropriate classes when I use it.
For example:
For this image, I’ve used the float-right class to align the image right. I’ve used the caption-right class to align my caption to the right. Float and align are the props from my image align component.
Don’t make a lot of work for yourself by repeating code. There was no need to split this into three components that all did similar things.