Eliminate the useEffect infinite loops in post.js

This commit is contained in:
Nat 2021-03-13 18:16:44 -04:00
parent f0b7fb2763
commit 764efd73ca
2 changed files with 19 additions and 30 deletions

View File

@ -1,36 +1,13 @@
import React, { useState, useEffect } from "react"; import React, { useState, useEffect } from "react";
import { import {
Image, Image,
Text,
View, View,
Dimensions, Dimensions,
TouchableWithoutFeedback TouchableWithoutFeedback
} from "react-native"; } from "react-native";
import { activeOrNot } from "src/interface/interactions"; import { activeOrNot } from "src/interface/interactions";
function invertField (field, state, updater) {
// Takes a function (like `setState`) and uses it to invert the given field of `state`
let newState = state;
newState[field] = !newState[field];
updater(newState);
}
// These callbacks will eventually make calls to the instance's API
function favouritedCallback(state, updater) {
invertField("favourited", state, updater);
}
function commentCallback(state, updater) {
invertField("commenting", state, updater);
}
function reblogCallback(state, updater) {
invertField("reblogged", state, updater);
}
function bookmarkCallback(state, updater) {
invertField("bookmarked", state, updater);
}
const PostActionJsx = (props) => { const PostActionJsx = (props) => {
return ( return (
<TouchableWithoutFeedback <TouchableWithoutFeedback
@ -77,20 +54,32 @@ const PostActionBarJsx = (props) => {
field = "favourited" field = "favourited"
pack = { icons.heart } pack = { icons.heart }
state = { state } state = { state }
callback = { () => favouritedCallback(state, setState) } /> callback = {
() => {
setState({ ...state, favourited: !state.favourited });
}
} />
<PostActionJsx <PostActionJsx
field = "reblogged" field = "reblogged"
pack = { icons.reblog } pack = { icons.reblog }
state = { state } state = { state }
callback = { () => reblogCallback(state, setState) } /> callback = {
() => {
setState({ ...state, reblogged: !state.reblogged });
}
} />
<PostActionJsx <PostActionJsx
field = "bookmarked" field = "bookmarked"
pack = { icons.bookmark } pack = { icons.bookmark }
last = { true } last = { true }
state = { state } state = { state }
callback = { () => bookmarkCallback(state, setState) } /> callback = {
() => {
setState({ ...state, bookmarked: !state.bookmarked });
}
} />
</View> </View>
) )
} }

View File

@ -175,7 +175,7 @@ export const PostByDataJsx = (props) => {
loaded: true loaded: true
}); });
}); });
}); }, []);
return ( return (
<View> <View>
@ -224,7 +224,7 @@ export const PostByIdJsx = (props) => {
}); });
}); });
})(); })();
}); }, []);
return ( return (
<View> <View>