SolidJSS
SolidJSโ€ข3y agoโ€ข
5 replies
Liquido

How can I set a style based on reactive state with fallback to props?

I want to change color of a button when active and return back to default value when not active:

const Internal = (props) => {
  const [clicked, setClicked] = createSignal(false);

  const style = mergeProps(props.style, { color: clicked() ? 'orange' : 'red' });

  return <button onClick={() => setClicked(c => !c)} style={style}>
    {props.children}
  </button>
}

function Counter() {
  return (
    <Internal style={{ color: 'blue', fontWeight: 'bold' }}>Click me</Internal>
  );
}


https://playground.solidjs.com/anonymous/c98af79a-5755-4edd-a7a4-f91d60c3ae02

How can I make the
style
value inside the
Internal
component reactive?
Quickly discover what the solid compiler will generate from your JSX template
Was this page helpful?