Home > Mobile >  React Native - 'target. value not working on mobile'
React Native - 'target. value not working on mobile'

Time:05-28

Greetings fellow humans,

I have a small problem with this little app that is just supposed to store and display data: https://snack.expo.dev/@vevlex/nullnotanobject

When I test this dircetly from my pc (I scan the QR code in my cmd window) on an ipad, the app boots fine but the data is not saved when re-running the app or when sending a job to another window (The "To Do" button on the right that shows up after you check a job).

When I run it from Expo, I get this error: null is not an object (evaluating 'jobs.map'

So I assume I'm doing something wrong with the saving / loading, or when actually writing the map, but I'm not sure, and I don't know how to approach this problem on my own.

Can anyone help me? Please and thank you.

Update: var temp1 is not being set, as the alert here returns "Unidentified" even tho I just define it in the previous line. This only happens on iOS and android, it works fine on the web sim. Can anyone explain this?

<TextInput
                  style={s.jobInput}
                  onChange={(e) => {
                    var temp1 = e.target.value;
                    alert(temp1)
                    setJobs((currentJobs) =>
                      produce(currentJobs, (v) => {
                        v[index].address = temp1;
                      })
                    );
                    saveItem();
                    
                  }}
                  value={j.address}
                  placeholder="Address"
                />

CodePudding user response:

You have the following code in loadItem.

const jsonValue = await AsyncStorage.getItem("@jobs");
const temp1 = JSON.parse(jsonValue);
setJobs(temp1);

The call to AsyncStorage.getItem is returning null which you are setting as the state variable, this is why you're receiving the error as you are trying to .map() over a value which is no longer an Array, but null.

If this behaviour is intended, you can fix it by simply adding a question mark after jobs. You can read more about this here.

jobs?.map((j, index)

CodePudding user response:

Ok after finding out what I stated in the update I searched a bit more and found out that on mobile you should use "onChangeText" rather than just "onChange".

  • Related