I want to create a dynamic function that returns me a variable from a string. I am using template string from ES6 but I don't know how to call this later. I need to pass a variable that keeps an object as a reference.
const checkDeviceWidth = type => {
if (documentWidth > 1200) return `${type}Options`;
if (documentWidth > 414) return `${type}MediumOptions`;
return `${type}MobileOptions`;
};
...
<Lottie
ref={backgroundImg}
isClickToPauseDisabled={true}
speed={1}
options={checkDeviceWidth('background')}></Lottie>
Returning object depending on the argument
const backgroundOptions = {
loop: true,
autoplay: true,
animationData: background.default,
};
const backgroundMediumOptions = {
loop: true,
autoplay: true,
animationData: backgroundMedium.default,
};
With this, I am getting an error that in options I must pass an object instead of a string. How to call this later without eval() function? I am working with React