Naming is quite subjective, to be honest, and everybody has their own opinions.
I’d actually prefer the word times
itself because it’s just a utility function, not a span maker at its core.
When I go through the code and see the times
function, I understand that this is a loop that will return an array, and each value will be whatever I return from the callback function, very similar to array map
function.
times
function is included in Lodash, so one more reason why I personally wouldn’t rename the function itself to SpanMaker
.
If I’m doing something not-so-obvious, I’d leave a comment there for my future self.
So my version will probably look something like this:
// ...
import { times } from '../path/to/utils'
/* import { times } from 'lodash' */ // You can get it from lodash also
// ...
export default function Crystal() {
// create array of 40 spans and divs
const foregroundSparkles = times(40, i => <span key={i} />)
const darkStripes = times(40, i => <div key={i} />) // Make some div soup
// ..
}
Again, it’s subjective. But, you’ll see things done somewhat in the above way.
Just to expand on this a little bit, if I needed to create a SpanMaker
component (function), I’d do it like so:
// `Spans` is just a React component
// On each iteration, it's going to run `spanProps`.
// Whatever is returned from the `spanProps`, will be used as the props for that span.
const Spans = ({ num, spanProps }) => {
let spans = []
for (let i = 0; i < num; i++) {
spans.push(<span {...spanProps(i)}></span>)
}
return spans
}
// Somewhere in your code
import Spans from '../path/to/Spans'
export default function Crystal() {
return (
<ScrollAnimation animateIn="fadeIn" animateOut="fadeOut" offset={100}>
<div className="Crystal">
<div id="foreGround">
<Spans num={40} spanProps={i => ({ key: i, onClick: () => alert('You clicked foreground span') })} />
</div>
<div id="darkStripes">
<Spans num={40} spanProps={i => ({ key: i, className: 'dark-stripes' })} />
</div>
<div id="stripes">
<Spans num={40} spanProps={i => ({ key: i })} />
</div>
</div>
</ScrollAnimation>
)
}
You can also see my opinions about naming and usage. Again, there are a lot of ways to skin a cat.