2

Hi guys I am working on a React Native Project. I'm a newbie to programming/React Native. I don't know how to put a textinput box in my modal on App.js. want to create an email textinput inside this modal Need some guidance and instructions. Help!

https://reactnative.dev/docs/textinput this is the example I am following. I am pasting it inside my app.js but it doesn't work.

import { StatusBar } from "expo-status-bar"; import React, { Component } from "react"; import { StyleSheet, Text, View, Modal, Button, Alert } from "react-native"; import UselessTextInput from "./components/TextInputComponent"; import Main from "./components/MainComponent"; import * as Font from "expo-font";



// const getFonts = () =>  //   Font.loadAsync({ //     "nunito-regular": require("./assets/fonts/Nunito-Regular.ttf"), //     "nunito-bold": require("./assets/fonts/Nunito-Bold.ttf"), //   });

class App extends Component {   constructor(props) {
    super(props);
    this.state = {
      fontsLoaded: false,
      showModal: false,
    };   }

  async loadFonts() {
    await Font.loadAsync({
      // Load a font `Montserrat` from a static resource
      "Nunito-Bold": require("./assets/fonts/Nunito-Bold.ttf"),

      // Any string can be used as the fontFamily name. Here we use an object to provide more control
      "Nunito-SemiBold": {
        uri: require("./assets/fonts/Nunito-SemiBold.ttf"),
        display: Font.FontDisplay.FALLBACK,
      },
    });
    this.setState({ fontsLoaded: true });   }   componentDidMount() {
    this.loadFonts();   }   render() {
    return (
      <View style={{ flex: 1, marginTop: 100 }}>
        <Button
          title="More recipes Click Here"
          onPress={() => {
            this.setState({ showModal: true });
          }}
        />
        <Main />
        <Modal transparent={true} visible={this.state.showModal}>
          <View style={{ backgroundColor: "#000000aa" }}>
            <View
              style={{
                backgroundColor: "#ffffff",
                margin: 50,
                padding: 40,
                borderRadius: 10,
              }}
            >
              <Text style={{ fontSize: 50 }}>Modal Text</Text>
              <Button
                title="No, Thanks."
                onPress={() => {
                  this.setState({ showModal: false });
                }}
              />
            </View>
          </View>
        </Modal>
      </View>
    );   } } export default App; const styles = StyleSheet.create({   container: {
    flex: 1,
    fontFamily: "Nunito-SemiBold",
    alignItems: "center",
    justifyContent: "center",   }, }); ```
1
  • Where is the textInput? Commented Mar 24, 2021 at 3:51

2 Answers 2

1

Just import from react-native and put in to the Modal as simple may help this.

import {
  TextInput,
} from 'react-native';

....
<Modal transparent={true} visible={this.state.showModal}>
...
 <TextInput
    style={styles.input}
    placeholder="useless placeholder"
    keyboardType="numeric"
  />
  ....
</Modal> 
  
 const styles = StyleSheet.create({
  input: {
    height: 40,
    margin: 12,
    borderWidth: 1,
  },
}); 

Sign up to request clarification or add additional context in comments.

Comments

0

Faced the same issue, it has something to do with setState re-rendering the whole component so the Modal being inside the component gets re-rendered. I moved the whole Modal into a separate file and had all its states in its own component file to fix the issue.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.