avatarCkmobile

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

4189

Abstract

pre><span class="hljs-attribute">placeholder</span><span class="hljs-operator">=</span>”Enter the username”</pre></div><div id="cf9e"><pre><span class="hljs-meta prompt_">/></span></pre></div><p id="0c65">We can also detect whenever the value changes whenever a user types a new character inside this field, we then take the current value that is inside the field and update our state.</p><p id="32ac">We add the “setUser” function to onChange, so it pass the current value to the function and update the state.</p><div id="3beb"><pre><TextInput</pre></div><div id="8e7a"><pre><span class="hljs-attribute">style</span><span class="hljs-operator">=</span>{styles.textinput}</pre></div><div id="15b1"><pre><span class="hljs-attribute">placeholder</span><span class="hljs-operator">=</span><span class="hljs-string">"Enter the username"</span></pre></div><div id="ee91"><pre><span class="hljs-attribute">onChange</span><span class="hljs-operator">=</span>{e<span class="hljs-operator">=</span>>setUser(e.target.value)}</pre></div><div id="53a1"><pre><span class="hljs-meta prompt_">/></span></pre></div><p id="6e6b">It should update in real time when we are typing inside the input field.

We can do for the second piece of state which is fruit. We need to duplicate the input field twice and change the placeholder for name and price.</p><div id="e7a4"><pre><TextInput</pre></div><div id="8b50"><pre><span class="hljs-attribute">style</span><span class="hljs-operator">=</span>{styles.textinput}</pre></div><div id="313c"><pre><span class="hljs-attribute">placeholder</span><span class="hljs-operator">=</span><span class="hljs-string">"Enter the username"</span></pre></div><div id="4d7c"><pre><span class="hljs-attribute">onChange</span><span class="hljs-operator">=</span>{e<span class="hljs-operator">=</span>>setUser(e.target.value)}</pre></div><div id="c09f"><pre><span class="hljs-meta prompt_">/></span></pre></div><div id="dfed"><pre><TextInput</pre></div><div id="02c1"><pre><span class="hljs-attribute">style</span><span class="hljs-operator">=</span>{styles.textinput}</pre></div><div id="f9b3"><pre><span class="hljs-attribute">placeholder</span><span class="hljs-operator">=</span><span class="hljs-string">"Enter the fruit"</span></pre></div><div id="9aaf"><pre>onChange = {<span class="hljs-attribute">e=>setFruit({...fruit,name</span>:e<span class="hljs-variable">.target</span><span class="hljs-variable">.value</span>})}</pre></div><div id="3487"><pre><span class="hljs-meta prompt_">/></span></pre></div><div id="ff5f"><pre><TextInput</pre></div><div id="017d"><pre><span class="hljs-attribute">style</span><span class="hljs-operator">=</span>{styles.textinput}</pre></div><div id="5271"><pre><span class="hljs-attribute">placeholder</span><span class="hljs-operator">=</span><span class="hljs-string">"Enter the price"</span></pre></div><div id="d988"><pre>onChange={<span class="hljs-attribute">e=>setFruit({...fruit,price</span>:e<span class="hljs-variable">.target</span><span class="hljs-variable">.value</span>})}</pre></div><div id="6d44"><pre><span class="hljs-meta prompt_">/></span></pre></div><p id="018e">Since the fruit is the object, so we need to use the spread operator to make a copy of that object, then update the value of that field.</p> <figure id="3f85"> <div> <div> <img class="ratio" src="http://placehold.it/16x9"> <iframe class="" src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2Ft4_vjPhlZyg%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3Dt4_vjPhlZyg&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2Ft4_vjPhlZyg%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" allowfullscreen="" frameborder="0" height="480" width="854"> </div> </div> </figure></iframe></div></div></figure><figure id="b015"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*34FltpDIou0-nilXHMuEhA.png"><figcaption></figcaption></figure><figure id="504e"><img src="https://cdn-images-1.r

Options

eadmedium.com/v2/resize:fit:800/1*JFqXTypunHcNKI__MTq6Xg.png"><figcaption></figcaption></figure><p id="1497">We can type in the user, fruit name and fruit price to update the state. Now we can see the sentence is “Peter is eating apple, which cost $9”.

Lastly, we can add the multi-line prop which allows us to add multiple different lines inside the text input.</p><p id="0ab7">The next prop is the keyboard type and we can set this equal to numeric. If we click on this, the keyboard that pops up should be numeric okay</p><div id="b1df"><pre><TextInput</pre></div><div id="2b5e"><pre><span class="hljs-attribute">style</span><span class="hljs-operator">=</span>{styles.textinput}</pre></div><div id="680d"><pre><span class="hljs-attribute">multiline</span></pre></div><div id="e916"><pre><span class="hljs-attribute">placeholder</span><span class="hljs-operator">=</span><span class="hljs-string">"Enter the fruit"</span></pre></div><div id="88da"><pre>onChange = {<span class="hljs-attribute">e=>setFruit({...fruit,name</span>:e<span class="hljs-variable">.target</span><span class="hljs-variable">.value</span>})}</pre></div><div id="08c9"><pre><span class="hljs-meta prompt_">/></span></pre></div><div id="f691"><pre><TextInput</pre></div><div id="3142"><pre><span class="hljs-attribute">style</span><span class="hljs-operator">=</span>{styles.textinput}</pre></div><div id="2c7f"><pre><span class="hljs-attribute">keyboardType</span><span class="hljs-operator">=</span><span class="hljs-string">"numeric"</span></pre></div><div id="ef33"><pre><span class="hljs-attribute">placeholder</span><span class="hljs-operator">=</span><span class="hljs-string">"Enter the price"</span></pre></div><div id="f8e4"><pre>onChange={<span class="hljs-attribute">e=>setFruit({...fruit,price</span>:e<span class="hljs-variable">.target</span><span class="hljs-variable">.value</span>})}</pre></div><div id="3b88"><pre><span class="hljs-meta prompt_">/></span></pre></div><p id="5af0">Related course:</p><div id="7de5" class="link-block"> <a href="https://www.udemy.com/course/complete-react-course-w-hooks-react-router-redux-usecontext/?referralCode=B192869617ADE079536F"> <div> <div> <h2>React - The Complete Guide with React Hook Redux 2021 in 4hr</h2> <div><h3>He is a registered teacher who has experience in teaching javascript, physics, maths and statistics for over 10 years…</h3></div> <div><p>www.udemy.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*elPwGFtGuvRBXBS3)"></div> </div> </div> </a> </div><p id="e308">Get Free tutorials on Youtube:</p><div id="05ae" class="link-block"> <a href="https://www.youtube.com/channel/UCu4-4FnutvSHVo9WHvq80Ww"> <div> <div> <h2>ckmobile</h2> <div><h3>JavaScript is very important now no matter you are working in backend or frontend. NodeJS, Angular, VueJS or React all…</h3></div> <div><p>www.youtube.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*I04ARIkwBabzhrJ6)"></div> </div> </div> </a> </div><p id="6361">Follow us:</p><p id="7b63"><a href="https://twitter.com/ckmobilejavasc1">https://twitter.com/ckmobilejavasc1</a></p><div id="294b" class="link-block"> <a href="https://www.linkedin.com/company/ckmobi/"> <div> <div> <h2>Ckmobile | LinkedIn</h2> <div><h3>Ckmobile | 30 followers on LinkedIn. Major focus on JavaScript tutorials for beginners and also different JavaScript…</h3></div> <div><p>www.linkedin.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*SuxMZfZfBat_Ff-w)"></div> </div> </div> </a> </div></article></body>

How to Use TextInput Component to Change State in React Native?

Creating input field to change the state.

In this article, we are going to talk about how to add a text input field so the user can type in something into that input field. We can take that value and update our state with that value.

Photo by Ferenc Almasi on Unsplash

Complete React Native articles:

  1. How to build a React Native app with Expo
  2. How to Run the Expo React Native app on the emulator
  3. Expo folder and file structure
  4. Expo React Native View, Text and Style
  5. Expo React Native — using React Hook useState()
  6. How to Use TextInput Component to Change State in React Native?
  7. How to Use Expo React Native List and ScrollView
  8. How and why we use Flatlist in Expo React Native
  9. What is TouchableOpacity in Expo React Native?

First, we need to use a component called “TextInput” , we need to import that at the top.

The input field is just a blank space. If we click into it we can see the cursor and the keyboard pop-up. So we need to add a style prop to this and set it equal to “styles.textinput”.

<TextInput style={styles.textinput}/>

Next we add the style at the bottom, this will be an object with different properties.

textinput:{
borderWidth:1,
borderColor:'black',
width:300,
height:50,
padding:10,
margin:10
},

Now we should see something that looks a bit more like a text input over here awesome, we type stuff in there now. We can also add a placeholder into this text input.

<TextInput
style={styles.textinput}
placeholder=”Enter the username”
/>

We can also detect whenever the value changes whenever a user types a new character inside this field, we then take the current value that is inside the field and update our state.

We add the “setUser” function to onChange, so it pass the current value to the function and update the state.

<TextInput
style={styles.textinput}
placeholder="Enter the username"
onChange={e=>setUser(e.target.value)}
/>

It should update in real time when we are typing inside the input field. We can do for the second piece of state which is fruit. We need to duplicate the input field twice and change the placeholder for name and price.

<TextInput
style={styles.textinput}
placeholder="Enter the username"
onChange={e=>setUser(e.target.value)}
/>
<TextInput
style={styles.textinput}
placeholder="Enter the fruit"
onChange = {e=>setFruit({...fruit,name:e.target.value})}
/>
<TextInput
style={styles.textinput}
placeholder="Enter the price"
onChange={e=>setFruit({...fruit,price:e.target.value})}
/>

Since the fruit is the object, so we need to use the spread operator to make a copy of that object, then update the value of that field.

We can type in the user, fruit name and fruit price to update the state. Now we can see the sentence is “Peter is eating apple, which cost $9”. Lastly, we can add the multi-line prop which allows us to add multiple different lines inside the text input.

The next prop is the keyboard type and we can set this equal to numeric. If we click on this, the keyboard that pops up should be numeric okay

<TextInput
style={styles.textinput}
multiline
placeholder="Enter the fruit"
onChange = {e=>setFruit({...fruit,name:e.target.value})}
/>
<TextInput
style={styles.textinput}
keyboardType="numeric"
placeholder="Enter the price"
onChange={e=>setFruit({...fruit,price:e.target.value})}
/>

Related course:

Get Free tutorials on Youtube:

Follow us:

https://twitter.com/ckmobilejavasc1

React Native
React
Mobile App Development
JavaScript
iOS
Recommended from ReadMedium