{"id":358,"date":"2019-10-22T15:45:38","date_gmt":"2019-10-22T15:45:38","guid":{"rendered":"https:\/\/sidroniolima.com.br\/blog\/?p=358"},"modified":"2019-10-22T15:48:10","modified_gmt":"2019-10-22T15:48:10","slug":"styled-components-flatlist-reactnative-css-problem","status":"publish","type":"post","link":"https:\/\/sidroniolima.com.br\/blog\/2019\/10\/22\/styled-components-flatlist-reactnative-css-problem\/","title":{"rendered":"Styled Components FlatList ReactNative CSS problem"},"content":{"rendered":"\n<div class=\"twitter-share\"><a href=\"https:\/\/twitter.com\/intent\/tweet?text=How%20to%20solve%20the%20StyledComponent%20Flatlist%20css%20problem%20when%20data%20is%20updated&#038;hashtags=styledcomponents%20%23reactnative%20%23css%20%23flatlist%20%23problem&#038;via=sidroniolima\" class=\"twitter-share-button\">Tweet<\/a><\/div>\n<p>Something happens when a FlatList decorated with Styled Components update your data: the CSS of the Content disappears.<\/p>\n<p>Bellow the original code and then the fix.<\/p>\n<div>\n<div>The CSS:<\/div>\n<div><\/div>\n<div><code>import styled from 'styled-components\/native';<\/code><\/div>\n<div><code>import DefaultButton from '~\/components\/Button';<\/code><\/div>\n<div>\n<div><code>export const Container = styled.View`<\/code><\/div>\n<div><code>flex: 1;<\/code><\/div>\n<div><code>background: #fff;<\/code><\/div>\n<div><code>border-radius: 4px;<\/code><\/div>\n<div><code>margin-bottom: 10px;<\/code><\/div>\n<div><code>`;<\/code><\/div>\n<div><code>export const Image = styled.Image`<\/code><\/div>\n<div><code>width: auto;<\/code><\/div>\n<div><code>height: 200;<\/code><\/div>\n<div><code>border-top-left-radius: 4px;<\/code><\/div>\n<div><code>border-top-right-radius: 4px;<\/code><\/div>\n<div><code>`;<\/code><\/div>\n<div><code>export const Content = styled.View`<\/code><\/div>\n<div><code>flex: 1;<\/code><\/div>\n<div><code>padding: 15px;<\/code><\/div>\n<div><code>`;<\/code><\/div>\n<div><code>export const Title = styled.Text`<\/code><\/div>\n<div><code>color: #333;<\/code><\/div>\n<div><code>font-size: 16px;<\/code><\/div>\n<div><code>font-weight: bold;<\/code><\/div>\n<div><code>margin: 10px 0;<\/code><\/div>\n<div><code>`;<\/code><\/div>\n<div><code>export const Info = styled.Text`<\/code><\/div>\n<div><code>color: #ccc;<\/code><\/div>\n<div><code>font-size: 12px;<\/code><\/div>\n<div><code>margin: 6px 0 6px 15px;<\/code><\/div>\n<div><code>`;<\/code><\/div>\n<div><code>export const Button = styled(DefaultButton)`<\/code><\/div>\n<div><code>margin-top: 10px;<\/code><\/div>\n<div><code>`;<\/code><\/div>\n<\/div>\n<\/div>\n<div><\/div>\n<div>The FlatList:<\/div>\n<div><\/div>\n<div>\n<div>\n<div><code>return (<\/code><\/div>\n<div><code>&lt;Background&gt;<\/code><\/div>\n<div><code>&lt;Container&gt;<\/code><\/div>\n<div><code>&lt;MeetupList<\/code><\/div>\n<div><code>data={subscriptions}<\/code><\/div>\n<div><code>keyExtractor={item=&gt;String(item.id)}<\/code><\/div>\n<div><code>renderItem={({item}) =&gt; (<\/code><\/div>\n<div><code>&lt;Meetup<\/code><\/div>\n<div><code>data={item.meetup}<\/code><\/div>\n<div><code>onHandle={() =&gt;handleCancel(item.id)}<\/code><\/div>\n<div><code>buttonText=\"Cancelar inscri\u00e7\u00e3o\"<\/code><\/div>\n<div><code>\/&gt;<\/code><\/div>\n<div><code>)}<\/code><\/div>\n<div><code>\/&gt;<\/code><\/div>\n<div><code>&lt;\/Container&gt;<\/code><\/div>\n<div><code>&lt;\/Background&gt;<\/code><\/div>\n<div><code>);<\/code><\/div>\n<\/div>\n<\/div>\n<div><\/div>\n<div>The fix is set the Content flex-shrink to auto in the Content styled component.<\/div>\n<div><\/div>\n<div>\n<div><code>export const Content = styled.View`<\/code><\/div>\n<div><code>flex: 1 <span style=\"color: #ff0000;\">auto<\/span>;<\/code><\/div>\n<div><code>padding: 15px;<\/code><\/div>\n<div><code>`;<\/code><\/div>\n<\/div>\n<div><\/div>\n\n<div class=\"twitter-share\"><a href=\"https:\/\/twitter.com\/intent\/tweet?text=How%20to%20solve%20the%20StyledComponent%20Flatlist%20css%20problem%20when%20data%20is%20updated&#038;hashtags=styledcomponents%20%23reactnative%20%23css%20%23flatlist%20%23problem&#038;via=sidroniolima\" class=\"twitter-share-button\">Tweet<\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Something happens when a FlatList decorated with Styled Components update your data: the CSS of the Content disappears. Bellow the original code and then the fix. The CSS: import styled from &#8216;styled-components\/native&#8217;; import DefaultButton from &#8216;~\/components\/Button&#8217;; export const Container = styled.View` flex: 1; background: #fff; border-radius: 4px; margin-bottom: 10px; `; export const Image = styled.Image` [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[53,52,69],"tags":[88,87,89],"class_list":["post-358","post","type-post","status-publish","format-standard","hentry","category-front-end","category-javascript","category-react-native","tag-flatlist","tag-reactnative","tag-styledcomponents"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/sidroniolima.com.br\/blog\/wp-json\/wp\/v2\/posts\/358","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sidroniolima.com.br\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sidroniolima.com.br\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sidroniolima.com.br\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sidroniolima.com.br\/blog\/wp-json\/wp\/v2\/comments?post=358"}],"version-history":[{"count":3,"href":"https:\/\/sidroniolima.com.br\/blog\/wp-json\/wp\/v2\/posts\/358\/revisions"}],"predecessor-version":[{"id":361,"href":"https:\/\/sidroniolima.com.br\/blog\/wp-json\/wp\/v2\/posts\/358\/revisions\/361"}],"wp:attachment":[{"href":"https:\/\/sidroniolima.com.br\/blog\/wp-json\/wp\/v2\/media?parent=358"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sidroniolima.com.br\/blog\/wp-json\/wp\/v2\/categories?post=358"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sidroniolima.com.br\/blog\/wp-json\/wp\/v2\/tags?post=358"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}