I’m rather new to react/JS therefore this question my sound somehow dumb:
I need multiple variables that are set depending on a location(System variable). Those variables should be constants as they should never change for a given instance.
This is my current solution, it works but it feels very wrong to define variables inside other variable declarations. It also seems to not be possible to declare footerWidth and footerAltText as constants without an initial value.
<Box
as='img'
width={footerWidth}
justifyContent='center'
alignContent='center'
src={footerLogoSource}
alt={footerAltText}
/>
Those attributes are provided by my features.ts:
export let footerWidth: string
export let footerAltText: string
export const footerLogoSource: string =
getAppConfig().contextPath +
(() => {
switch (getAppConfig().landesvariante) {
case Variante.SN:
footerWidth = '30%'
footerAltText = 'lorem ipsum'
return '/assets/kofin_eu_logo.svg'
case Variante.BB:
footerWidth = '20%'
footerAltText = 'lorem ipsum 2'
return '/assets/logo_bb.svg'
default:
return null
}
})()
One solution would be to make 3 switch-case configurations for the 3 variables, but that would lead to alot of duplicated code…
Is there a proper way to do this?
2
Answers
Here’s an approach using array destructuring
You can use object destructuring as well
You can also do it without any destructering or self invoking functions and with some additional typing