S
SolidJS2y ago
Kyros

Add props to an element programmatically

In one of my components, I pass an element as a prop, although I'd like to add the class argument to it in order to style it.
import styles from "./css/MyElement.module.css";

interface Props {
src: JSX.Element;
}

export default function MyElement(props: Props) {
return (
<div class={styles.wrapper}>
{props.src} // props.src.addAttribute("class", {styles.my_class}) or smt would be nice, or anything that achieves the same result too
</div>
);
}
import styles from "./css/MyElement.module.css";

interface Props {
src: JSX.Element;
}

export default function MyElement(props: Props) {
return (
<div class={styles.wrapper}>
{props.src} // props.src.addAttribute("class", {styles.my_class}) or smt would be nice, or anything that achieves the same result too
</div>
);
}
5 Replies
Kyros
KyrosOP2y ago
update: I was able to get it to work by passing a function definition that accepted a css_class argument and then calling that inside the component, although i find this approach quite tedious and id really appreciate a better solution...
// Parent.tsx
import { MyComponent, ComponentImPassing } from "./components";


export default function ParentComponent() {
return (
<div>
<MyComponent path="/" src={function (css_class: string) { return <ComponentImPassing class={css_class}></ComponentImPassing> }} />
</div>
)
}

// MyComponent.tsx
import styles from "./css/MyElement.module.css";
import { JSX } from "solid-js";

interface Props {
src: (arg0: string) => JSX.Element;
}

export default function MyElement(props: Props) {
return (
<div class={styles.wrapper}>
{props.src(styles.svg)}
</div>
);
}
// Parent.tsx
import { MyComponent, ComponentImPassing } from "./components";


export default function ParentComponent() {
return (
<div>
<MyComponent path="/" src={function (css_class: string) { return <ComponentImPassing class={css_class}></ComponentImPassing> }} />
</div>
)
}

// MyComponent.tsx
import styles from "./css/MyElement.module.css";
import { JSX } from "solid-js";

interface Props {
src: (arg0: string) => JSX.Element;
}

export default function MyElement(props: Props) {
return (
<div class={styles.wrapper}>
{props.src(styles.svg)}
</div>
);
}
belst
belst2y ago
you can use <Dynamic> for this
Kyros
KyrosOP2y ago
what is it?
Kyros
KyrosOP2y ago
ty oh exactly what i needed, ty
Want results from more Discord servers?
Add your server