Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recoil mastered #2

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
360 changes: 194 additions & 166 deletions 7.4-async-queries/package-lock.json

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion 7.4-async-queries/public/vite.svg

This file was deleted.

59 changes: 27 additions & 32 deletions 7.4-async-queries/src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,40 +1,35 @@
import React from "react";

import './App.css'
import { RecoilRoot, useRecoilState, useRecoilValue, useSetRecoilState } from 'recoil'
import { notifications, totalNotificationSelector } from './atoms'
import { useEffect } from 'react'
import axios from 'axios'
import { useRecoilValue, RecoilRoot } from "recoil";
import { dataFetchFromSource, totalNotifs } from "./atoms";

function App() {
return <RecoilRoot>
<MainApp />
</RecoilRoot>
return (
<div>
<RecoilRoot>
<React.Suspense fallback={<h1>Loading...</h1>}>
<Data />
</React.Suspense>
</RecoilRoot>
</div>
);
}

function MainApp() {
const [networkCount, setNetworkCount] = useRecoilState(notifications)
const totalNotificationCount = useRecoilValue(totalNotificationSelector);
export default App;

useEffect(() => {
// fetch
axios.get("https://sum-server.100xdevs.com/notifications")
.then(res => {
setNetworkCount(res.data)
})
}, [])
function Data() {
const state = useRecoilValue(dataFetchFromSource);

return (
<>
<button>Home</button>
<button>My network ({networkCount.networks >= 100 ? "99+" : networkCount.networks})</button>
<button>Jobs {networkCount.jobs}</button>
<button>Messaging ({networkCount.messaging})</button>
<button>Notifications ({networkCount.notifications})</button>

<button>Me ({totalNotificationCount})</button>
</>
)
const { networkCount, jobsCount, messagesCount, notificationCount } = state;
const TotalNumOfNotifs = useRecoilValue(totalNotifs);
return (
<div>
<button>Home</button>
<button>My Network ({networkCount > 100 ? "99+" : networkCount})</button>
<button>Jobs ({jobsCount})</button>
<button>Messaging ({messagesCount})</button>
<button>Notifications ({notificationCount})</button>
<button>Me ({TotalNumOfNotifs})</button>
</div>
);
}

export default App
1 change: 0 additions & 1 deletion 7.4-async-queries/src/assets/react.svg

This file was deleted.

50 changes: 32 additions & 18 deletions 7.4-async-queries/src/atoms.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,36 @@
import { atom, selector } from "recoil";

export const notifications = atom({
key: "networkAtom",
default: {
network: 4,
jobs: 6,
messaging: 3,
notifications: 3
}
export const urlAtom = atom({
key: "urlToFetchData",
default: "http://localhost:3000/",
});
export const getData = selector({
key: "fetchData",
get: async ({ get }) => {
const url = get(urlAtom);
const response = await fetch(url);
if (!response.ok) {
throw response.error;
}
const data = await response.json();
return data;
},
});

export const dataFetchFromSource = atom({
key: "dataFetchFromSource",
default: getData,
});

export const totalNotificationSelector = selector({
key: "totalNotificationSelector",
get: ({get}) => {
const allNotifications = get(notifications);
return allNotifications.network +
allNotifications.jobs +
allNotifications.notifications +
allNotifications.messaging
}
})
export const totalNotifs = selector({
key: "totatlNotifs",
get: ({ get }) => {
const obj = get(getData);
const total =
obj.notificationCount +
obj.messagesCount +
obj.jobsCount +
obj.networkCount;
return total;
},
});
Loading