#react-router-dom tutorial issues
62 messages · Page 1 of 1 (latest)
🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord
🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize
✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)
import { Form, useLoaderData } from "react-router-dom";
import { getContact } from "../contacts";
export async function loader({ params }) {
const contact = await getContact(params.contactId);
return { contact };
}
export default function Contact() {
const { contact } = useLoaderData();
return (
<div id="contact">
<div>
<img key={contact.avatar} src={contact.avatar || null} />
</div>
<div>
<h1>
{contact.first && contact.last ? (
<>
{contact.first} {contact.last}
</>
) : (
<i>No Name</i>
)}{" "}
<Favorite contact={contact} />
</h1>
{contact.twitter && (
<p>
<a target="_blank" href={`https://twitter.com/${contact.twitter}`}>
{contact.twitter}
</a>
</p>
)}
{contact.notes && <p>{contact.notes}</p>}
<div>
<Form action="edit">
<button type="submit">Edit</button>
</Form>
<Form
method="post"
action="destroy"
onSubmit={(event) => {
if (!confirm("Please confirm you want to delete this record.")) {
event.preventDefault();
}
}}
>
<button type="submit">Delete</button>
</Form>
</div>
</div>
</div>
);
}
function Favorite({ contact }) {
let favorite = contact.favorite;
return (
<Form method="post">
<button
name="favorite"
value={favorite ? "false" : "true"}
aria-label={favorite ? "Remove from favorites" : "Add to favorites"}
>
{favorite ? "★" : "☆"}
</button>
</Form>
);
}
import localforage from "localforage";
import { matchSorter } from "match-sorter";
import sortBy from "sort-by";
export async function getContacts(query) {
await fakeNetwork(`getContacts:${query}`);
let contacts = await localforage.getItem("contacts");
if (!contacts) contacts = [];
if (query) {
contacts = matchSorter(contacts, query, { keys: ["first", "last"] });
}
return contacts.sort(sortBy("last", "createdAt"));
}
export async function createContact() {
await fakeNetwork();
let id = Math.random().toString(36).substring(2, 9);
let contact = { id, createdAt: Date.now() };
let contacts = await getContacts();
contacts.unshift(contact);
await set(contacts);
return contact;
}
export async function getContact(id) {
await fakeNetwork(`contact:${id}`);
let contacts = await localforage.getItem("contacts");
let contact = contacts.find((contact) => contact.id === id);
return contact ?? null;
}
export async function updateContact(id, updates) {
await fakeNetwork();
let contacts = await localforage.getItem("contacts");
let contact = contacts.find((contact) => contact.id === id);
if (!contact) throw new Error("No contact found for", id);
Object.assign(contact, updates);
await set(contacts);
return contact;
}
export async function deleteContact(id) {
let contacts = await localforage.getItem("contacts");
let index = contacts.findIndex((contact) => contact.id === id);
if (index > -1) {
contacts.splice(index, 1);
await set(contacts);
return true;
}
return false;
}
function set(contacts) {
return localforage.setItem("contacts", contacts);
}
// fake a cache so we don't slow down stuff we've already seen
let fakeCache = {};
async function fakeNetwork(key) {
if (!key) {
fakeCache = {};
}
if (fakeCache[key]) {
return;
}
fakeCache[key] = true;
return new Promise((res) => {
setTimeout(res, Math.random() * 800);
});
}
<ul>
{contacts.map((contact) => (
<li key={contact.id}>
<NavLink
to={`contacts/${contact.id}`}
className={({ isActive, isPending }) =>
isActive ? "active" : isPending ? "pending" : ""
}
>
{contact.first || contact.last ? (
<>
{contact.first} {contact.last}
</>
) : (
<i>No Name</i>
)}{" "}
{contact.favorite && <span>★</span>}
</NavLink>
</li>
))}
</ul>
) : (
<p>
<i>No contacts</i>
</p>
)}``` this is the part where it display contact right?
yes
it's displaying the i message instead of something that matches whatever the search was
can you console.log contacts
where do i put that
have you check what thus contacts contains already ?
contacts
do a useEffect checking for contact and console.log contacts
console.log(contacts)
}, [contacts]);```
where at in which file do i need to put that
ohhh on the gist one
can you add in there !==0
instead of the ?
no just add it
nexto to contacts.length
contacts.length !== 00 ?
no . contacts.length !==0 ?
This question has been marked as answered! If you have any other questions, feel free to create another post
[Click here](#1148973418822254653 message)
did it work ?
yes
ok cool
No offense, but you should understand the code instead of just seeing and typing
agree and i do for the mostpart
lmao
Then, I think you should probably learn some basics
the $ threw me for a loop yesterday
no what he meant is that any length should be compared to a number and not boolean
when you leave it with that it would be considered a boolean which is false which lead to the condition to fall in no contacts
almost everything i plan to make could be made with just html and css, react just makes it easier
But js basics are important
whats the best source to learn that
youtube
i've been following docs to learn react and a little nextjs
want 100 seconds ?https://www.youtube.com/watch?v=DHjqpvDnNGE&pp=ygULamF2YXNjcmlwdCA%3D
JavaScript is the the programming language that built the web. Learn how it evolved into a powerful tool for building websites, servers with Node.js, mobile apps, desktop software, and more https://fireship.io/courses/javascript/
#js #programming #100SecondsOfCode
🔗 Resources
History of JavaScript https://youtu.be/Sh6lK57Cuk4
How JavaScript W...
i like how even in a 100 sec version you can't learn one thing without 20 other things being thrown at you
hahah
i try and learn html and when i hit a wall i get told to learn react
just google stuff .w3school is one if you want to read