CSSとReactJSで条件付きにクラスを動的に追加する方法
ReactJSで手動クラス名に動的にクラスを追加するには、いくつかの方法があります。
方法
- className属性を使う
const MyComponent = () => {
const [active, setActive] = useState(false);
const handleClick = () => {
setActive(true);
};
return (
<button
className={`my-button ${active ? 'active' : ''}`}
onClick={handleClick}
>
Click me
</button>
);
};
この例では、active
状態に基づいて動的にactive
クラスを追加しています。
- classList APIを使う
const MyComponent = () => {
const [active, setActive] = useState(false);
const handleClick = () => {
setActive(true);
};
const ref = useRef();
useEffect(() => {
const element = ref.current;
if (active) {
element.classList.add('active');
} else {
element.classList.remove('active');
}
}, [active]);
return (
<button
ref={ref}
onClick={handleClick}
>
Click me
</button>
);
};
- Styled Componentsを使う
import styled from 'styled-components';
const MyButton = styled.button`
&.active {
color: red;
}
`;
const MyComponent = () => {
const [active, setActive] = useState(false);
const handleClick = () => {
setActive(true);
};
return (
<MyButton
onClick={handleClick}
>
Click me
</MyButton>
);
};
この例では、Styled Componentsを使用して、active
状態に基づいて動的にスタイルを適用しています。
- シンプルなケースでは、
className
属性を使うのが最も簡単です。 - より複雑なケースでは、
classList
APIを使うのがより柔軟性があります。 - スタイルを動的に適用したい場合は、Styled Componentsを使うのが良いでしょう。
const MyComponent = () => {
const [active, setActive] = useState(false);
const handleClick = () => {
setActive(true);
};
return (
<button
className={`my-button ${active ? 'active' : ''}`}
onClick={handleClick}
>
Click me
</button>
);
};
const MyComponent = () => {
const [active, setActive] = useState(false);
const handleClick = () => {
setActive(true);
};
const ref = useRef();
useEffect(() => {
const element = ref.current;
if (active) {
element.classList.add('active');
} else {
element.classList.remove('active');
}
}, [active]);
return (
<button
ref={ref}
onClick={handleClick}
>
Click me
</button>
);
};
import styled from 'styled-components';
const MyButton = styled.button`
&.active {
color: red;
}
`;
const MyComponent = () => {
const [active, setActive] = useState(false);
const handleClick = () => {
setActive(true);
};
return (
<MyButton
onClick={handleClick}
>
Click me
</MyButton>
);
};
動作確認
上記のコードをReactプロジェクトにコピーして、実行してみてください。
説明
- 最初のコードでは、
className
属性にテンプレートリテラルを使用して、my-button
クラスとactive
状態に基づいてactive
クラスを動的に追加しています。
CSSとReactJSで手動クラス名に動的にクラスを追加する他の方法
const MyComponent = () => {
const [active, setActive] = useState(false);
const ref = useRef();
const handleClick = () => {
setActive(true);
};
useEffect(() => {
const element = ref.current;
if (active) {
element.classList.add('active');
} else {
element.classList.remove('active');
}
}, [active]);
return (
<button ref={ref} onClick={handleClick}>
Click me
</button>
);
};
この例では、ref
を使用してボタン要素への参照を取得し、useState
を使用してactive
状態を追跡しています。active
状態が変化したときに、useEffect
フックを使用して、classList
APIを使用して動的にクラスを追加または削除します。
useRefとuseCallbackを使う
const MyComponent = () => {
const [active, setActive] = useState(false);
const ref = useRef();
const handleClick = useCallback(() => {
setActive(true);
}, []);
useEffect(() => {
const element = ref.current;
if (active) {
element.classList.add('active');
} else {
element.classList.remove('active');
}
}, [active]);
return (
<button ref={ref} onClick={handleClick}>
Click me
</button>
);
};
この例では、useCallback
を使用して、handleClick
関数を安定化しています。これにより、useEffect
フックが不要になり、パフォーマンスが向上します。
第三者ライブラリを使う
classnames
などのライブラリを使用して、クラス名を動的に管理することができます。
import classnames from 'classnames';
const MyComponent = () => {
const [active, setActive] = useState(false);
const handleClick = () => {
setActive(true);
};
return (
<button
className={classnames('my-button', {
active,
})}
onClick={handleClick}
>
Click me
</button>
);
};
- パフォーマンスが重要な場合は、
useRef
とuseCallback
を使うのが良いでしょう。 - 複雑なケースでは、第三者ライブラリを使うのが便利です。
css reactjs