Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6287

TypeError: setEmail is not a function. (In 'setEmail(input)', 'setEmail' is undefined) (unable to use hooks with context api)

$
0
0

App.js

  const[getScreen, showScreen] = useState(false);  const[getEmail, setEmail] = useState("");<LoginScreen/><LoginContexts.Provider value={{getEmail,setEmail,getScreen,showScreen}}>{showScreen?<Screen2/>:<LoginScreen/> }</LoginContexts.Provider>

Login Screen

   import { StyleSheet, Text, TextInput, View, Button } from 'react-native';import { NavigationContainer } from '@react-navigation/native';import { createNativeStackNavigator } from '@react-navigation/native-stack';import { useState, useContext } from 'react';import { LoginContexts } from '../Contexts/LoginContexts'function LoginScreen (){const {getEmail,setEmail,getScreen,showScreen} = useContext(LoginContexts);return(<View style={styles.container}><Text style={styles.loginText}>Login</Text><View style={styles.textInput}><TextInput  placeholder='Please enter your email'            defaultValue={getEmail}            onChange={(input) =>{setEmail(input)}}/></View>          **^^^^^^^^^^^^^^^^^^^^^Problem lies here^^^^^^^^^^^^^^^**<View style={styles.space}/><View style={styles.space}/><View><Button style={styles.loginBTN} title="Login"            onPress={() => {showScreen(true)}} /></View></View>    );

}

**Context API **

import { createContext} from "react";export const  LoginContexts = createContext({});

Screen 2

const {getEmail} = useContext(LoginContexts);return (<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}><LoginContexts.Consumer>    {(fname)=> {<Text>{fname}</Text>;    }}</LoginContexts.Consumer>

When I am entering a text then program is giving me an error that setEmail is not a function.I have taken out setEmail from useContext. My aim is to display text taken from user to another screen on button click


Viewing all articles
Browse latest Browse all 6287

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>