<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Sean Ye</title>
    <description>The latest articles on DEV Community by Sean Ye (@danovity).</description>
    <link>https://dev.to/danovity</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F164092%2F1bc18686-0e09-4524-b4e4-249062db3f02.jpeg</url>
      <title>DEV Community: Sean Ye</title>
      <link>https://dev.to/danovity</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/danovity"/>
    <language>en</language>
    <item>
      <title>How To Make A Simple Hero Banner In React Native</title>
      <dc:creator>Sean Ye</dc:creator>
      <pubDate>Mon, 05 Aug 2019 15:00:34 +0000</pubDate>
      <link>https://dev.to/danovity/how-to-make-a-simple-hero-banner-in-react-native-3el1</link>
      <guid>https://dev.to/danovity/how-to-make-a-simple-hero-banner-in-react-native-3el1</guid>
      <description>&lt;p&gt;Today I would like to write a short tutorial on how to make a React Native hero banner that has a linear gradient layer. &lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1, Make a banner image using &lt;code&gt;FullWidthImage&lt;/code&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Create a project using codesandbox's &lt;code&gt;react-native-web&lt;/code&gt; template, remove the unused components
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Image&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;StyleSheet&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;View&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react-native&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;logoUri&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://imagesvc.timeincapp.com/v3/fan/image?url=https://raptorsrapture.com/wp-content/uploads/getty-images/2016/04/1094224730.jpeg&amp;amp;w=1600&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;render&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;View&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Image&lt;/span&gt;
          &lt;span class="nx"&gt;accessibilityLabel&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;React logo&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
          &lt;span class="nx"&gt;source&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{{&lt;/span&gt; &lt;span class="na"&gt;uri&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;logoUri&lt;/span&gt; &lt;span class="p"&gt;}}&lt;/span&gt;
          &lt;span class="nx"&gt;resizeMode&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;contain&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
          &lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;styles&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;heroHeaderImage&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/View&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;styles&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;StyleSheet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;create&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;heroHeaderImage&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;80&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  The Problem
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;we can only resize the image's width by controlling its height &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/rn-herobanner1-8j1py"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h3&gt;
  
  
  Solution
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Use &lt;code&gt;FullWidthImage&lt;/code&gt; instead &lt;code&gt;Image&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;FullWidthImage&lt;/code&gt; is a package that allows &lt;strong&gt;a responsive Image element that takes the full width of its parent element while maintaining aspect ratio&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;It has a neat built-in feature called &lt;code&gt;automatic detection&lt;/code&gt;, which &lt;strong&gt;can automatically detect the aspect ratio of remote images, all you need to provide is the uri as you would do with the regular Image component.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Which is what we are going to use.&lt;/p&gt;

&lt;p&gt;Just like that, we have a hero banner.&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/rn-herobanner2-nos19"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2, Add and Position HeroHeaderText
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Wrap the &lt;code&gt;heroHeaderText&lt;/code&gt; components with &lt;code&gt;heroHeaderContainer&lt;/code&gt; in order to position both components simultaneously&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;heroContainer&lt;/code&gt; will have &lt;code&gt;position: relative&lt;/code&gt;, this will allow any children components that have a &lt;code&gt;position: absolute&lt;/code&gt; to use &lt;code&gt;heroContainer&lt;/code&gt; as a reference point, in this case, we are adding &lt;code&gt;position: absolute&lt;/code&gt; to the &lt;code&gt;heroHeaderContainer&lt;/code&gt;,  (note: even though we do not need to add &lt;code&gt;position: relative&lt;/code&gt; to work, but if there are parent elements of &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/rn-herobanner3-ribij"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3, Add Linear Gradient
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;To make the &lt;code&gt;heroHeaderText&lt;/code&gt; more visible, one common practice is to add a linear gradient to the background image, the React Native approach of adding the linear gradient is different than how you would on the web 
&lt;strong&gt;we are using &lt;code&gt;react-native-web-linear-gradient&lt;/code&gt; because we are using the &lt;code&gt;react-native-web&lt;/code&gt; template on codesandbox, for &lt;code&gt;react-native&lt;/code&gt; apps, please use &lt;a href="https://www.npmjs.com/package/react-native-linear-gradient"&gt;react-native-linear-gradient&lt;/a&gt;&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;position: absolute&lt;/code&gt; and &lt;code&gt;zIndex&lt;/code&gt; to position the linear gradient on top of the &lt;code&gt;Hero Banner&lt;/code&gt; image&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/rn-herobanner4-l36r0"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 4, Add A heroSubheader
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;To add more "flare" to our awesome hero banner, we will add a hero subheader using the &lt;code&gt;Button&lt;/code&gt; component from &lt;code&gt;react-native&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Finished, &lt;iframe src="https://codesandbox.io/embed/rnherovanvleet-mcz62"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

</description>
      <category>reactnative</category>
    </item>
  </channel>
</rss>
