最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - React - Styled Component with multiple classes - Stack Overflow

programmeradmin2浏览0评论

I'm about to build a small project in react using styled ponent, however, I have a question for using this package. I would like to create a styled ponent that brings together several classes but I don't know how to do, for example :

<div class="search-input search-input-small">

How to turn it into a styled ponent ? In css there are two different classes, but in styled ponent i don't know.

Thank you.

I'm about to build a small project in react using styled ponent, however, I have a question for using this package. I would like to create a styled ponent that brings together several classes but I don't know how to do, for example :

<div class="search-input search-input-small">

How to turn it into a styled ponent ? In css there are two different classes, but in styled ponent i don't know.

Thank you.

Share Improve this question asked Nov 14, 2020 at 20:51 user4671973user4671973 3
  • I think you need to think about the bined styles of both classes, and use props to adjust your styling dynamically. What do these classes do in normal CSS? – BlackMath Commented Nov 14, 2020 at 21:44
  • we can get @Aib Codes Daily example, .search-input { background-color: blue; } .search-input-small { color: white; } – user4671973 Commented Nov 15, 2020 at 10:52
  • First define const SearchInput = styled.div` backgroundColor: blue;` and then define const SearchInputSmall= styled(SearchInput)` color: white;` and use the SearchInputSmall styled ponent in place of the div with multiple classes. – BlackMath Commented Nov 15, 2020 at 13:45
Add a ment  | 

2 Answers 2

Reset to default 6

There are a few ways to do this. For example, you could run with the base styling on a styled ponent and then "upgrade that with the additional styling.

const Div = styled.div`
     /// styles for className -> search-input
    }`
const SmallDiv = styled(Div)`
    /// styles for className -> search-input-small
   `

Also, if you know SASS you can structure your CSS with nesting using &. e.g.

 const Div = styled.div`
     .search-input {
          //CSS
          &-small {
             //CSS
           }
         
         }
     `

With styled-ponents, you can go with a similar format.

Here we are creating a styled ponent named Div, within the styled ponent I've declared classes .search-input and .search-input.search-input-small to style the div from your example.

The Div is being treated as a parent container.

const Div = styled.div`

display: flex;
background-color: black; 

.search-input {
background-color: blue;
}
.search-input.search-input-small {
color: white;

}
`;

We call the newly created styled ponent like this:

export default function App() {
  return (
      <Div>
      <div className="search-input search-input-small">Hello</div>
      </Div>
  
  );
}

As you can see the <div class="search-input search-input-small"> from your example is a child to the parent Div. The parent Div does not require styles but I have included them for this example.

Also here is a codesandbox with a working example.

发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>