2

I'm trying to get to learn hooks and react native in general.

Currently I'm trying to create a small shopping list app, where you'd add your desired item to the list.

I already have the input + button on the display.

<View>
    <TextInput placeholder="hinzufügen..." 
         onChangeText={inputHandler} 
         value={enteredEntity} 
     />

     <Button title="+" onPress={addInputHandler} />
</View>

I have two functions + hook declared as the following:

const [enteredEntity, setEnteredEntity] = useState('');

const inputHandler = (enteredEntity) => {
    setEnteredEntity(enteredEntity);
}

const addInputHandler = () => {
    console.log(enteredEntity);
}

and get the following error message (see below) - at Line 20, where the error should be I have the TextInput.

enter image description here

Thank you very much for your input.

3
  • 5
    Please show the entire code. Did you imported useState? import React, { useState } from 'react' Commented Aug 14, 2019 at 13:28
  • Thank you Ian, this solved the issue for me. I was totally missing the import. Commented Aug 14, 2019 at 13:29
  • import React, {useState} from 'react' Commented Aug 10, 2020 at 12:14

3 Answers 3

1

As Ian suggested in his comment, the issue was due to the missing import of { useState }.

import React, { useState } from 'react';
Sign up to request clarification or add additional context in comments.

Comments

0

This is how hooks can be implemented. This example implemented with componentDidUpdate. Depending on the second argument of useEffect this hook can act as componentDidMount or componentDidUpdate. useEffect runs every render is done, that could cause infinite loop so always try to pass 2nd argument

    import React, {useState, useEffect}  from 'react';

    const [userBalance, setUserBalance] = useState(session.userBalance);
    useEffect(()=>{
                setUserBalance(session.userBalance)
            },[session.userBalance]);

Comments

0

import React, {useState} from 'react'

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.