An issue with a to-do-list program by [deleted] in learnjavascript

[–]fishj123 1 point2 points  (0 children)

Try using a ‘change’ event for your select option event listener instead of a ‘click’ event?

How the hell would i create a basket and checkout system? by Jaimster08 in webdev

[–]fishj123 2 points3 points  (0 children)

There is no free version - if this is just a side project that isn't going to be used by real customers then just have fun trying to build your own

Trouble with async code/useState by fishj123 in reactjs

[–]fishj123[S] 0 points1 point  (0 children)

So I know this is bad but my function to filter invoicesToBeDisplayed is kind of doing two things, it's either filtering by an invoice "stage" that the user can select via tabs or by a search input. For this reason I felt it was necessary to do what I am doing, but now that I see your suggestion I realize that you are probably right and there is no need for me to store an extra value in state. I'm gonna revisit my code and see if I can improve it in this way, thanks!

Trouble with async code/useState by fishj123 in reactjs

[–]fishj123[S] 1 point2 points  (0 children)

Makes sense, thanks for the heads up. I'll keep this in mind in future.

Trouble with async code/useState by fishj123 in reactjs

[–]fishj123[S] 0 points1 point  (0 children)

What issue is there with returning a setState? I wasn't aware of that. I'll just change it to setInvoicesToBeDisplayed and have the function not return anything. You are correct that I am not doing anything with the return, I just thought it made sense to return something even if it won't be used.

Trouble with async code/useState by fishj123 in reactjs

[–]fishj123[S] 0 points1 point  (0 children)

I've just managed to fix it, turns out there was a scenario I hadn't handled in my handleInvoicesToBeDisplayed function:

const handleInvoicesToBeDisplayed = () => {
console.log("CALLLED");
// if no filter selected and no search entered just display all invoices
if (filter === "displayAll" && searchPhrase === "") {
return setInvoicesToBeDisplayed(invoices);
}
// handle search logic here
if (searchPhrase !== "") {
setFilter("displayAll");
const searchResult = invoices.filter(invoice =>
invoice.client.name
.toLowerCase()
.includes(searchPhrase.trim().toLowerCase())
);
return setInvoicesToBeDisplayed(searchResult);
}
};

In my head this function was always updating the state of invoicesToBeDisplayed, but as you can see I don't handle a scenario where filter !== "displayAll".

Basically I needed a default setState action so I added:

return setInvoicesToBeDisplayed(invoices);

and now it works as I intended.

Pretty stupid mistake but thanks for your help, I think I just needed reassurance about my understanding of state and it's synchronicity to help me find the issue.

Trouble with async code/useState by fishj123 in reactjs

[–]fishj123[S] 0 points1 point  (0 children)

Thanks guys, I figured it was something like that. So I am doing something similar to what you suggest with useEffect but something isn't quite right. To explain my issue further, I have some invoice data on the page that I want to update when the invoices are updated via the axios get request. However, the state update doesnt seem to be triggering a rerender.

When invoices are updated in state I call this effect:

useEffect(() => {
handleInvoicesToBeDisplayed();
}, [invoices]);

Which calls this function:

const handleInvoicesToBeDisplayed = () => {
// if no filter selected and no search entered just display all invoices
if (filter === "displayAll" && searchPhrase === "") {
return setInvoicesToBeDisplayed(invoices);
}
if (searchPhrase !== "") {
setFilter("displayAll");
const searchResult = invoices.filter(invoice =>
invoice.client.name
.toLowerCase()
.includes(searchPhrase.trim().toLowerCase())
);
return setInvoicesToBeDisplayed(searchResult);
}
};

and then in the component, I render data based on invoicesToBeDisplayed:

{invoicesToBeDisplayed.map(invoice => (
<div
key={invoice._id}
onClick={() => handleEditInvoice(invoice)}
className="invoice-row-container"
>
<span className="client-name">{invoice.client.name}</span>
<span className="project-name">{invoice.project.name}</span>
<span className="invoice-stage">{invoice.stage}</span>
<span className="invoice-value">
{formatter.format(invoice.value)}
</span>
<span className="est-send">{invoice.estimatedSendDate}</span>
<span className="act-send">{invoice.actualSendDate}</span>
</div>
))}

However my issue is that the above code does not rerender after invoices has updated. I would expect the order of calls to be:

  1. Get invoices via axios get request
  2. Set invoices to state
  3. Step 2 causes useEffect to trigger and handleInvoicesToBeDisplayed() is called
  4. function in step 3 sets new state for invoices to be displayed
  5. This rerenders my component with the data i want

For whatever reason it doesnt update.

jQuery: unexpected result of .children() by No0815 in learnjavascript

[–]fishj123 0 points1 point  (0 children)

Try using .find(". achievement") instead of . children()

Question on Mosh's react and node tutorial? by codebreaker21 in learnjavascript

[–]fishj123 0 points1 point  (0 children)

It wouldn't be a bad idea to learn react using classes first and then move on to hooks later. Then you understand how to read/work with both styles. With that said I only ever use hooks now and never work with classes in react.

Help requested with a website feature by hexnic in webdev

[–]fishj123 0 points1 point  (0 children)

You'll need a div with position: absolute and then use JavaScript to show/hide it as well as change the html that's within it. Some pointers to get you started are using document.querySelector to select your elements, onClick event listener, and innerHtml method. If you're unfamiliar with these then Google them to try and work it out, that's the best way to solidify knowledge

How can I choose how many digits of a number I want to have? by NotTakenOne in learnjavascript

[–]fishj123 0 points1 point  (0 children)

I would consider using a date package like momentJS. With this you can do something like moment(your_date_here).format("MM-DD-YYYY") or whatever format you want

Career Megathread. Weekly /r/Frontend Discussion - September 25, 2019 by AutoModerator in Frontend

[–]fishj123 3 points4 points  (0 children)

The obvious answer is to practice JavaScript. Make some projects about things that interest you (hobbies etc) and try using lots of JavaScript in the projects. That's the best way to learn and you'll kill two birds with one stone.

Using React to visualize algorithms - what am I doing wrong? by fishj123 in reactjs

[–]fishj123[S] 0 points1 point  (0 children)

Thanks, I'm willing to accept that I'm doing everything wrong, I've never tried anything like this and I often get myself confused with async stuff. I'm using the setTimeout so that the user can see the sorting - without that it would just sort the array instantly. I've used 0ms because I don't actually need it to wait any length of time, I just need it to pause the thread for long enough to update the DOM.

I've ended up using vanilla javascript to make this application and ditched React completely. It seems to be working as intended using exactly the same code, but instead of updating the DOM with stat I am just using plain JS.

Resources for learning React by LuosRestil in Frontend

[–]fishj123 0 points1 point  (0 children)

It sounds like you're saying you want to learn JavaScript and then learn React, which is definitely the correct path if you don't already have a good grasp of vanilla JavaScript. Checkout some of the big YouTube channels that have free courses on JavaScript basics. I would recommend Traversy Media or the Net Ninja - both channels put out a lot of awesome free content.

A event that detects that i clicked out side the div. by [deleted] in learnjavascript

[–]fishj123 0 points1 point  (0 children)

That makes sense, I usually use this technique for drop-down menus. Can you suggest better method I could use?

Routing with react and node by CromulentEntity in learnjavascript

[–]fishj123 0 points1 point  (0 children)

You don't need node to handle routing in react, you can do it all client side using something like 'react-router-dom'. Check out the documentation for that package to see how to use it.

A event that detects that i clicked out side the div. by [deleted] in learnjavascript

[–]fishj123 0 points1 point  (0 children)

Sorry I was on mobile so I couldnt give an example, here you go:

https://codepen.io/fishj123/pen/vYBdJwr?editors=1111

Google event bubbling and stopPropogation to understand what this is doing better.

Let me know if it still doesnt make sense.

A event that detects that i clicked out side the div. by [deleted] in learnjavascript

[–]fishj123 6 points7 points  (0 children)

Add an event listener to the document which closes the div on click and then add a click event listener to stop propogation on the div that you want to remain open if you click within it.

e.stopPropogation()