frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

Show HN: GitHub Action to track views, stars, and clones on my projects

https://github.com/timf34/github-engagement-stats
1•timf34•47s ago•0 comments

ASRock Rack AMPONEMD12DNO for AmpereOne M Arm Servers Shown at Computex 2025

https://www.servethehome.com/asrock-rack-amponemd12dno-for-ampereone-m-arm-servers-shown-at-computex-2025/
1•rbanffy•55s ago•0 comments

Not Another Travel Planning App

https://travelaiplanner.com/
1•drisw•2m ago•0 comments

Ex150-14 review: More of the Same

https://www.exfatloss.com/p/ex150-14-review-more-of-the-same
1•paulpauper•4m ago•0 comments

CoInterviewer: A Hackathon Product

https://anilturaga.github.io/cointerviewer
1•anorak27•5m ago•0 comments

Quantum visualisation techniques could bring fault-tolerant quantum computers

https://www.ox.ac.uk/news/2025-05-30-new-quantum-visualisation-techniques-could-accelerate-arrival-fault-tolerant-quantum
1•gnabgib•6m ago•0 comments

Show HN: Purpose Reminders – One simple, positive act emailed monthly to all

https://purposereminders.com
1•purposereminder•7m ago•0 comments

Show HN: Dungeon Crawler on Demand Fork

https://7underlines.itch.io/quick-dungeon-crawler-on-demand
1•logTom•9m ago•0 comments

Planner: Generating Diversified Paragraph via Latent Language Diffusion Model

https://arxiv.org/abs/2306.02531
1•nathan-barry•9m ago•0 comments

White House Planning to Replace Musk's NASA Administrator Nominee

https://www.bloomberg.com/news/articles/2025-05-31/white-house-planning-to-replace-nasa-administrator-nominee
1•JumpCrisscross•9m ago•0 comments

Peter Kuper's Graphic Novel, Where the Insects Draw Us

https://www.thenation.com/article/environment/ecosystem-environment-biosphere-nature/
1•petethomas•11m ago•0 comments

Show HN: Watch by Team – Movie/Show Discovery through their creators

https://watchby.team
1•MCtheperson•12m ago•0 comments

Embedding Godot games in iOS became easy

https://christianselig.com/2025/05/godot-ios-interop/
1•goranmoomin•13m ago•0 comments

SweRank: Software Issue Localization with Code Ranking

https://arxiv.org/abs/2505.07849
1•PaulHoule•13m ago•0 comments

Review: Jesse Armstrong's 'Mountainhead' imagines a tech bro-pocalypse

https://www.sfchronicle.com/entertainment/movies-tv/article/mountainhead-review-20351688.php
2•andromaton•15m ago•1 comments

I don't use HDMI, and I never will

https://www.xda-developers.com/i-dont-use-hdmi-and-i-never-will/
2•ch0wn•15m ago•0 comments

A Tutorial on Meta-Reinforcement Learning

https://arxiv.org/abs/2301.08028
1•Anon84•16m ago•0 comments

SpaceX town warns residents they may lose right to use property

https://www.cnbc.com/2025/05/29/elon-musk-spacex-starbase-texas.html
2•doener•19m ago•0 comments

Gridnotes – an infinite 2D text editor

https://sidsite.com/posts/gridnotes/
1•montebicyclelo•22m ago•0 comments

Artificial Intelligence (1983)

https://archive.org/details/ine0000unse
1•starkparker•23m ago•0 comments

250M Honeybees Escape After a Truck Rolls over in Washington State

https://www.nytimes.com/2025/05/30/us/washington-bees-escape-truck-crash.html
1•whack•25m ago•0 comments

The Post-Human Revolution

https://posthumanrevolution.substack.com/p/coming-soon
2•judgementday•25m ago•0 comments

Create a React and Flask Project in 2025

https://blog.miguelgrinberg.com/post/create-a-react-flask-project-in-2025
1•kdamica•29m ago•0 comments

Trump pulls Isaacman nomination for space

https://arstechnica.com/space/2025/05/trump-pulls-isaacman-nomination-for-space-source-nasa-is-fed/
4•LorenDB•30m ago•0 comments

Dare to be great (2006) [pdf]

https://www.oaktreecapital.com/docs/default-source/memos/2006-09-07-dare-to-be-great.pdf
1•simonebrunozzi•35m ago•0 comments

Whole-genome sequencing suggests novel genetic factors associated w Alzheimer's

https://www.nature.com/articles/s41467-025-59949-y
2•bookofjoe•36m ago•0 comments

Flash - A shell parser, formatter, and interpreter written in Rust

https://github.com/raphamorim/flash
1•bananzajr•38m ago•0 comments

White House to pull NASA nominee Isaacman

https://www.semafor.com/article/05/31/2025/white-house-expected-to-pull-nasa-nominee-isaacman
7•cratermoon•38m ago•1 comments

Google Maps can't explain why it falsely labeled German autobahns as closed

https://arstechnica.com/tech-policy/2025/05/google-maps-cant-explain-why-it-falsely-labeled-german-autobahns-as-closed/
3•Eduard•40m ago•0 comments

Ask HN: What is best practice for passing in public keys to API?

1•jaidenlee•41m ago•0 comments
Open in hackernews

React's useState should require a dependency array

https://bikeshedd.ing/posts/use_state_should_require_a_dependency_array/
9•jaaamesey•1d ago

Comments

SebastianKra•1d ago
> Call it contrived, but maybe we want to add drawings to our tasks. With different brush colours

That's when you lift state up - either to the parent component, or put the value in local storage. Both as a user and as a developer, my mental model is that each item in the list has it's own form, rather than sharing one.

I haven't seen enough instances of this problem that I think it would warrant a change to the core api's. Maybe you could demonstrate more real-world examples?

A pattern that I sometimes like, is an internal state that is set only if the input is dirty. This has the advantage of preventing external updates from interrupting the user while they're in the middle of typing.

    function NumberInput({ value, setValue }) {
        const [tempValue, setTempValue] = useState(null)
        
        function submit() {
           const parsedValue = parseInt(tempValue)
           setTempName(null)
           if (parsedValue !== NaN) 
               setItemName(tempName)
        }
        
        return <Input value={tempValue ?? value} onChange={setTempValue} onBlur={submit} />
    }
PaulHoule•1d ago
I think in the cases where you'd want useState to have declared dependencies you don't want to use useState.

The people who started the system I work on now had the bad habit of copying prop variables into state variables for no good reason and over a period of time I've removed those state variables, pushed state up, and followed the mantra that "props are preferable to state".

merb•1d ago
What you now basically did is an antipattern. Because your example gets quite messy if the outer state gets updated and the component should render the new value. I mean even react itself acknowledges the problem.

https://react.dev/learn/you-might-not-need-an-effect#updatin...

It’s basically that useEffect gets overused because the state hook is such a bad api.

SebastianKra•14h ago
It's not. Try some inputs in Figma or basically any numeric input:

- Focus an Input that only accepts numerical values

- Enter some non-numeric text

- Focus back out

My pattern is fairly common when you pay attention to it.

I'm not arguing that you should always use it; that's why I didn't replicate OP’s example. It’s just a demonstration that most cases have a solution that describes the intention more accurately than a dependency array.

The remaining cases can be solved by the workaround from react.dev or a userland alternative.

merb•10h ago
Well what you did will often not stay as is and at least evolve into the antipattern (update the input from outside) the only alternative would be to create a new key to do that. And trust me there is a lot of useeffect Code which is not a good idea
jaaamesey•18h ago
I'd agree that generally, using `key` to reset state at the component tree level is fine, and that good component structure or persisting state through other means makes it a lot harder for this issue to come up.

> my mental model is that each item in the list has it's own form, rather than sharing one

100% - it makes things a lot easier to reason about if you're guaranteed to have a fresh instance of a component for each item being switched to. In fact, if I saw that stale state issue come up again in production, and it wasn't the kind of thing avoiding state entirely could fix, my suggestion would just be `key` instead of reaching for a third party hook.

I wouldn't be suggesting a change to React itself if that change also didn't enforce that way of thinking, albeit at the hook level instead of component tree level. `key` always felt like a weird escape hatch to me in that:

- it's not a solution lint rules would nudge someone towards

- `key` needs to be applied by the consumer of the component, not the component itself

- we lose all state in the tree and re-create all DOM nodes

I'll admit that it's hard finding good examples of where that last thing is an issue. The only other one off the top of my head might be preserving component/DOM state between route changes, e.g. keeping the same scroll position in a list of filters between similar screens for searching products. These issues only really come up when you have a deep component tree and lifting components up gets much trickier than having state reset naturally and atomically.

SebastianKra•13h ago
Okay I see where you're coming from.

> if that change also didn't enforce that way of thinking

My gut says that your mental model would lead to overall more spaghetti than the current one.

I also think that remounting all the DOM nodes is the correct default, since it clears any uncontrolled input and open popovers. Any shared settings between routes/forms etc. are custom behavior for user-convenience and should require additional code.

> needs to be applied by the consumer of the component, not the component itself

Depends on how you look at it. In my mental model, the client is actually requesting multiple form components, each with a different default value. Like 50 list cells where only one is visible. It's not a coincidence that lists and state-resets are implemented with the same api. But if you want to colocate this behavior, there's still the option to build a small wrapper:

function FormWrapper({ item }) {

    return <FormInner key={item.id} … />

}
unconed•1d ago
I've written a similar `useDerivedState` hook before, which is basically formalizing the lastValue !== value / setLastValue pattern that the docs teach you.

But there is a major blind spot. The reason you want the dependencies is to reset the state when the outside demands it. But only way you can reset such a state is if the dependency _changes_. So it's not possible to reset the state back to the _same_ value as before.

To do that, you either need to manually manage the component lifecycle with a `key={..}` on the outside, or, you need to add e.g. a `version={N}` counter as an extra prop, to handle the edge case. Except, at that point, it makes more sense to rely on `version` entirely.

The 'proper' solution I've found is to actually write code to do what the policy and etiquette demands. E.g. for a number input, you can't aggressively reformat the contents because that's unusable, but you can check if the current text parses to the same value as before (if it is a valid number). There is no way to hide this sort of nuance with a generic state hook, it's too context-specific.

What is most useful is to treat the bridge between controlled and uncontrolled state as its own wrapper component, e.g. called <BufferedInput>, which has a render prop to actually render the <input> itself. It accepts a `value / onChange` on the outside, but passes on a different `text / onChange` to the inside. Give it a `parse`, `format` and `validate` prop that take functions, and you can clean up a lot of messy input scenarios.