Rewrote app to use hooks and fixed multiple re-renders.

This commit is contained in:
Patrick Fic
2019-12-11 18:39:27 -08:00
parent 5c7523e6bd
commit f333301f67
16 changed files with 162 additions and 193 deletions

View File

@@ -1,6 +1,4 @@
import React, { Component } from "react";
import { Mutation, Query } from "react-apollo";
import { GET_CURRENT_USER, SET_CURRENT_USER } from "../graphql/local.queries";
import App from "./App";
import Spin from "../components/loading-spinner/loading-spinner.component";
@@ -72,29 +70,17 @@ class AppContainer extends Component {
}
middlewares.push(authLink.concat(link));
const cache = new InMemoryCache({ addTypename: false });
const cache = new InMemoryCache();
const client = new ApolloClient({
link: ApolloLink.from(middlewares),
cache,
typeDefs,
resolvers,
defaultOptions: {
watchQuery: {
fetchPolicy: "cache-and-network",
errorPolicy: "ignore"
},
query: {
fetchPolicy: "cache-and-network",
errorPolicy: "all"
}
}
resolvers
});
client.writeData({
data: {
...initialState
}
data: initialState
});
try {
@@ -125,24 +111,7 @@ class AppContainer extends Component {
return (
<ApolloProvider client={client}>
<Query query={GET_CURRENT_USER}>
{({ loading, error, data: { currentUser } }) => {
if (loading) return <Spin />;
if (error) return error.message;
return (
<Mutation mutation={SET_CURRENT_USER}>
{setCurrentUser => (
<App
currentUser={currentUser}
setCurrentUser={user => {
setCurrentUser({ variables: { user } });
}}
/>
)}
</Mutation>
);
}}
</Query>
<App />
</ApolloProvider>
);
}