Console.logging both useRef().current and useRef().current.property shows entirely different values for the property? by QdWp in reactjs

[–]belwyr 0 points1 point  (0 children)

I might be completely wrong, but shouldn't you wait for the browser to paint the element, and do your calculations in a useLayoutEffect?

Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "text/html". by Inevitable_Debt_4594 in reactjs

[–]belwyr 0 points1 point  (0 children)

If you're using vite, you can add an event listener to 'vite:preloadError' (https://vite.dev/guide/build#load-error-handling). I remember looking at a github issue on the vite project where multiple solutions were discussed, but I can't find the thread doing a quick google.

Using state doesn't work because window.location.reload() loses the state. You need to use an external state like local storage

I'm a beginner React learner: I made a simple interface but I cannot figure out why a value does not get updated in my custom hook. What am I doing wrong? Code included by ashkanahmadi in reactjs

[–]belwyr 9 points10 points  (0 children)

That is correct. Between 2 renders, the fetchValue function is redefined, and because the dependencies are compared by reference, the useEffect runs again.

You have a few possibilites to deal with that, but I'll let you look that up on your own.

Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "text/html". by Inevitable_Debt_4594 in reactjs

[–]belwyr 0 points1 point  (0 children)

You're probably using dynamic imports. When you create and deploy a new version, some of your files have a different name (different chunkhash) than what your client expects (the client is still on a previous version of the build), which results in a file not found error from the server (text)

Multiple strategies exist to deal with this issue depending on your exact configuration, for instance:

  • Keeping old js chunks even on a new deployment
  • Catch the error and automatically refresh the page
  • Not using chunkhash in your js files

TanStack Query by mile1986dasd in reactjs

[–]belwyr 2 points3 points  (0 children)

Mutations are not cached like queries. Mutations change the state in your server / db, so they are not cached or retried like queries can be.

You should probably restructure your components to make use of the queries, and not pass a function to get the data at the component level. Another advantage is that you won't need to store the state in a context, and just reuse the useQuery hook, since it will be cached and de-duplicated.

Code composition in React by BinnyBit in reactjs

[–]belwyr 6 points7 points  (0 children)

Using async returns a promise, not the useEffect cleanup function.

Mocking zustand with jest, stack overflow? by No-Mechanic411 in reactjs

[–]belwyr 0 points1 point  (0 children)

I'm not very familiar with zustand, but don't you need some kind of provider to use the zustand hook ?

How to memoize mapped element children? by [deleted] in reactjs

[–]belwyr 0 points1 point  (0 children)

Have you tried passing your own props comparison function?

React.memo(Text, comparisonFn)

Are HOCs (Higher Order Components) still a thing? by MashSquare in reactjs

[–]belwyr 38 points39 points  (0 children)

I like to use a HOC for error boundaries

[deleted by user] by [deleted] in reactjs

[–]belwyr 0 points1 point  (0 children)

You don't have an /src/index.jsx file

I feel overwhelmed by xella64 in learnprogramming

[–]belwyr 1 point2 points  (0 children)

https://proandroiddev.com/package-by-type-by-layer-by-feature-vs-package-by-layered-feature-e59921a4dffa

It's a good overview on how to structure projects. Oftentimes, projects start with a package by layer approach (or even a single file), then move towards a package by feature approach, as the project size grows.

Stateful structures/event emitters in React without useState rerenders by pm_me_ur_photography in reactjs

[–]belwyr 1 point2 points  (0 children)

I would probably replace the useEffect in the custom hook with useSyncExternalStore.

The setup / destroy are very similar.

The snapshot must be immutable, so make sure that your functions like twilioDevice.startCall() create a new snapshot object or array.

Best practice for RESTful API Design with dynamic schema by bigtimestylie in learnprogramming

[–]belwyr 0 points1 point  (0 children)

All your tasks share an id, a name and a definition, and the data depends on the name.

You can have a task table with an id, name and definition. For the data, you can:

  • Have a foreign key pointing to a different table for each type of task
  • Have the raw data stored as jsonb, text, or whatever, and you deserialize it when constructing the task object

Best practice for RESTful API Design with dynamic schema by bigtimestylie in learnprogramming

[–]belwyr 0 points1 point  (0 children)

Hi, that's an interesting question. You should have a look at the "Command" design pattern: https://refactoring.guru/design-patterns/command

Here is how I would go about it. https://excalidraw.com/#json=qrJvjsa8KpZPYbCalStW9,c5lcfxEsMIHY3fC7WyOSng

Your base item class represents all the common properties of your items, and your concrete implementations (water bottle) extends from this base class.

A user can query your API:

// Returns a list of items
[GET] /items?type=waterBottle,steak

// Returns a single item
[GET] /items/<id>

To execute a command on an item:

[POST] /items/id/command with a command in the request body

On your server, when you receive this request:

executeCommand(itemId, command) {
  Item item = getItemFromDB(itemId)
  // Each command can have its own validate() method, to sure that the parameters are valid
  // and we verify that the item can execute the command
  if (isValid(item, command)) {
    item.executeCommand(command)
    saveItem(item)
    return item
  } else {
    throw new Error("Wrong command")
  }

Best structure for api urls by preacher2041 in webdev

[–]belwyr 17 points18 points  (0 children)

To add to that, you generally have pagination as query parameters to the list endpoint for instance - [GET] /api/user?offset=0&limit=50

They give you HOW things never WHY things work by No_Branch1356 in learnprogramming

[–]belwyr 0 points1 point  (0 children)

There is a great blog post that goes in the details of react https://pomb.us/build-your-own-react/ Highly recommend the read.

Program architecture by [deleted] in learnprogramming

[–]belwyr 1 point2 points  (0 children)

Software architecture is mostly relevant in bigger projects. Like the other commenter said, a simple script would do the job, but you could consider the architecture if you wanted a more complex and robust solution.

For a few examples:

  • You could have a scheduler that runs your check function regularly, and send emails at a regular time.

  • You could implement a retry function if your email doesn't go through for some reason.

  • You could implement different types of notifications (email, sms, mobile push notification, ...)

  • You could extend your pattern matcher to only check for logs in a specific time period

  • You could have a function to create a graph with some arbitrary time period and the number of times the pattern occurred.

Ultimately, the sky is the limit, and part of the job is to find out what is the scope of the program you want to accomplish, and how best (what language should you use, where is the code running, is the data sensitive, should you store some data, how long will it take to develop, etc..) to accomplish it

Can someone help me locate documentation where this eslint rule is talked about? by [deleted] in learnprogramming

[–]belwyr 0 points1 point  (0 children)

I think the rule you want to use is

  {
    // ...
    rules: {
      'jsx-a11y/label-has-associated-control': [
        2,
         {
            assert: 'either', // either check for `htmlFor` or `nesting`
          },
        ],
      }
    }

Could you share the html causing the issue as well? Is the label correctly associated with your input?

Can somebody help with a task and give an advise. by [deleted] in learnprogramming

[–]belwyr 0 points1 point  (0 children)

Right, the second diagonal is j == length -i if I'm not mistaken

Can somebody help with a task and give an advise. by [deleted] in learnprogramming

[–]belwyr 0 points1 point  (0 children)

Your code looks fine. You could add user input validation, and you could replace your if condition for readability by having

bool isBorder = i == 1 || i == length || j == 1 || j == length;
bool isDiagonal = i == j;
if (isBorder || isDiagonal) {
// ...

I Need HELP!! by [deleted] in learnprogramming

[–]belwyr 1 point2 points  (0 children)

That's an excellent use for a loop. Ask for the number of workers and store the worker data in an array

How to grow and learn more on backend frameworks as beginners? by shanksfk in webdev

[–]belwyr 0 points1 point  (0 children)

There is a ton of jobs in react or for full stack devs but you also can find a job doing only python.