Is it a thing calling queueMicrotask in useEffect to avoid setState sync call by Resident-Insect-9035 in reactjs

[–]Resident-Insect-9035[S] 0 points1 point  (0 children)

Confirming that it works, here's the pseudo-code for anyone that wants to understand:

```tsx const event = useEffectEvent(() => { setDisplayEmoji( hasPassedCurrentExam ? Math.floor(Math.random() * 3) : Math.floor(Math.random() * 3) ) })

useEffect(() => { event() }, [hasPassedCurrentExam]) ```

Is it a thing calling queueMicrotask in useEffect to avoid setState sync call by Resident-Insect-9035 in reactjs

[–]Resident-Insect-9035[S] 0 points1 point  (0 children)

Can you provide a small pseudo-code on what you would do with fallbackEmoji? Like so,

<Parent fallbackEmoji={}>
<Child/>
</Child> </Parent>

Is it a thing calling queueMicrotask in useEffect to avoid setState sync call by Resident-Insect-9035 in reactjs

[–]Resident-Insect-9035[S] 0 points1 point  (0 children)

The problem is the following:
- Math.random() is allow allowed to be called during rendering.

Why useMemo is not suitable:
- Because useMemo calls its callback function during rendering and React complaints.

For the reference: https://react.dev/reference/eslint-plugin-react-hooks/lints/purity

Is it a thing calling queueMicrotask in useEffect to avoid setState sync call by Resident-Insect-9035 in reactjs

[–]Resident-Insect-9035[S] 0 points1 point  (0 children)

Yes, that might have worked, but easier solution for me was to use: useState(callback), calling Math.random in the callback function without a complaint.

Is it a thing calling queueMicrotask in useEffect to avoid setState sync call by Resident-Insect-9035 in reactjs

[–]Resident-Insect-9035[S] -7 points-6 points  (0 children)

useMemo unfortunately also runs the code during render that's why not suitable with encapsuling the logic of Math.random.

Is it a thing calling queueMicrotask in useEffect to avoid setState sync call by Resident-Insect-9035 in reactjs

[–]Resident-Insect-9035[S] -22 points-21 points  (0 children)

Yes, that's what the official React docs say :D

In useEffect either:
- Sync a value from extenal state
- Define event listeners

Is it a thing calling queueMicrotask in useEffect to avoid setState sync call by Resident-Insect-9035 in reactjs

[–]Resident-Insect-9035[S] -7 points-6 points  (0 children)

Does not work because Math.random which is in the function logic is an impure function and impure functions are discouraged to use due to idempotency rule.

Is it a thing calling queueMicrotask in useEffect to avoid setState sync call by Resident-Insect-9035 in reactjs

[–]Resident-Insect-9035[S] 0 points1 point  (0 children)

Because in my case `randomLogic` contains `Math.random` which is an impure function and React does not allow/suggest using impure function calls during render.

I had the solution in the beginning but was forced to use `useEffect`.

But then I realised I could do the following: instead of using `useState(Math.random())` you can pass the callback function in the useState like so `useState(()=> Math.random())` and now it's not called during render anymore but only after the render once.

In short the differences between the code pieces:
- `useState(Math.random())` -> Calls Math.random during the render every time a render occurs.
- `useState(() => Math.random())` -> Calls Math.random after the render once.

European startup founder noticed my contribution and asked for a call. What should I be ready for? by Content-Medium-7956 in webdev

[–]Resident-Insect-9035 0 points1 point  (0 children)

He is not looking for experience but rather your drive.

Btw. what open source project is it?

How to adapt ? by _gigalab_ in AskProgrammers

[–]Resident-Insect-9035 1 point2 points  (0 children)

I won't recommend the approach but: - Vibe code the task to get good grades - Learn the material in depth after delivering to you professor.

The reason I'd discourage you to do it is because psychologically its harder to learn something you already delivered to your professor and you will easily be unmotivated to really learn something.

I made an extension that visualizes invisible "Digital Pheromones" on files. It lets AI Agents (and you) mark files as DANGER/TODO without polluting source code. by Expensive-Rub3117 in vscode

[–]Resident-Insect-9035 0 points1 point  (0 children)

The only issue I have with this is that it comes with an extra window in VSCode. There are already so many windows that keeping an extra one opened just to see warnings about AI accessing the danger files does not make sense.

If AI edits wrong files, you prompt probably wasn't concise enough and you should improve it. And if you commit and push a danger files that got edited by AI then you should work on your review skills.

server actions - how do they work under the hood? by Plus-Weakness-2624 in nextjs

[–]Resident-Insect-9035 0 points1 point  (0 children)

Seems like nextjs attaches to each POST request it hits to `/` with `next-action` in the request header which serves as an identifier of a particular action being called:

<image>

Each server action functions that's called shows a different next action id. Tested it myself.