0

I'm trying to input text to innerState.feedType. but when i tried it says Cannot read property 'toString' of undefined How can i fix my code?

this is my code

     const [innerState, setInnerState] = useState<any>({
       feedType: 'bbong', 
        })


     const onChangeEtcfeedtype = useCallback((text) => {
          setInnerState( innerState['feedType'] = text)
       },[]);

        <TextInput
            placeholder="input."
            value={innerState.feedType}
            onChangeText={onChangeEtcfeedtype}
          />
2
  • can you also share the part you used toString please? Commented Jun 16, 2022 at 8:42
  • @frankie303 example ("hi") Commented Jun 16, 2022 at 8:49

3 Answers 3

1

You need to add null-safety(?) operator, because state creates after render components. Also need to return new link on object for re-render component.

 const [innerState, setInnerState] = useState<any>({
   feedType: 'bbong', 
    })


 const onChangeEtcfeedtype = (text) => {
      setInnerState({...innerState, innerState.feedType: text});
   };

    <TextInput
        placeholder="input."
        value={innerState?.feedType}
        onChangeText={onChangeEtcfeedtype}
      />
Sign up to request clarification or add additional context in comments.

Comments

0

Here, innerState['feedType'] = text you are changing the state directly and then store it using the updater function, you should not change it directly.

You have innerState which is object and it contains feedType property. So, update a state you have to pass the object with the same key with new value.

Correct Way:

setInnerState({feedType: text})

Comments

0

I think this is how you should write it. About the error. It look like its not come from this part. It come from your <TextInput /> component and possiblely caused because what you doing with onChange event.

const [innerState, setInnerState] = useState<any>({
       feedType: 'bbong', 
     })


     const onChangeEtcfeedtype = useCallback((text) => {
          setInnerState({feedType: text})
       },[]);

        <TextInput
            placeholder="input."
            value={innerState.feedType}
            onChangeText={onChangeEtcfeedtype}
          />

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.