Closed yarliganfatih closed 1 year ago
As an alternative to these, I will update the data only with AsyncStorage and make it state variables that pull the data from AsyncStorage when the components are focused.
Component {
const [data, setData] = useState(preData);
const _setDatas = (_data) => { // _setData will now be used instead of setData in the component
AsyncStorage.setItem("data", JSON.stringify(_data)) // for sync
setData(_data)
}
const isFocused = useIsFocused();
useEffect(() => {
if (isFocused) {
Promise.resolve(asyncSetData()).catch((e) => { throw e; });
}
}, [isFocused]);
const asyncSetData = async() => {
const _data = await AsyncStorage.getItem("data")
if (_data) setData(JSON.parse(_data))
}
}
First I created a structure like this
I haven't had any problems so far.
Then I tried to create another similar structure.
Everything worked fine until here, Even the new chat has been added to the chats variable. I noticed a hidden method that converts the chats variable to its previous state while it will redirect to the full chat screen. Therefore, new chat could not be added. I couldn't find the source of that method.
Although the chats variable changes to its previous state when calling the setChats method in the addChat function, it does not happen in the updateChats function I mentioned earlier.