reactによるフロントエンド開発をしています。
emotionのstyled-components記法を用いているのですが、他のコンポーネントでstyleを上書きしても反映されません。ソースコードは以下になります。
上書きされるコンポーネント
typescript
1import React from 'react' 2import { Button } from '@mui/material' 3import styled from '@emotion/styled' 4 5type Props = { 6 children: string; 7 onClick: VoidFunction; 8} 9 10const BaseButton: React.FC<Props> = (props) => { 11 const { children, onClick} = props; 12 13 return ( 14 <StyledButton 15 variant="outlined" 16 onClick={onClick}> 17 {children} 18 </StyledButton> 19 ) 20} 21 22export default BaseButton 23 24const StyledButton = styled(Button)({ 25 margin: '15px', 26 fontSize: '1.6em', 27 })
上書きをしているコンポーネント
typescript
1import React from 'react' 2import styled from '@emotion/styled' 3import BaseButton from '../Atom/Button/BaseButton' 4import { useNavigate } from 'react-router-dom' 5 6const MainButton = () => { 7 8 const navigate = useNavigate() 9 const toList = () => {navigate('/List')} 10 11 return ( 12 <div> 13 <SButton size="large" onClick={toList}>リスト</SButton> 14 </div> 15 ) 16} 17 18export default MainButton 19 20const SButton = styled(BaseButton)({ 21 color: 'black', 22})
この場合、ボタンの色が黒色に変わらず、元のコンポーネントのボタンが表示されます。
ご回答のほどよろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。