question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

More complex examples or good/bad practices documented

See original GitHub issue

Hey! Awesome work! I hope it’s gonna be a part of React 🤞.

To be precise: I’m not raising issue, rather feature request - could you please extend documentation or add more complex examples? Especially part about good/bad practices. For example how to access function/objects stored in context because those must have same reference in order to skip update. Example - component is getting function from context:

const addListener = useContextSelector(
    ListenerContext,
    listeners => listeners.addListener
)

Provider looks like:

const [listeners, setListeners] = useState([])
const [state, setState] = useState({}) // some other state
const addListener = (listener) => {
    // some complex logic 
    setListeners(newListeners)
}

const state = {
    state,
    addListener,
}

return (
    <Provider value={state}>
        {children}
    </Provider>
)

Therefore as compare condition is:

(ref.current.v === nextValue || Object.is(ref.current.s, ref.current.f(nextValue)))

Update will be triggered. However it might be fixed by useRef:

const { current: addListener } = useRef((listener) => {
    // some complex logic 
    setListener(newListeners)
})

I think there are more examples of how to use/how to not use this hook and it might be helpful to document it.

Cheers!

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:10 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
JoviDeCroockcommented, Oct 31, 2019

I have converted to my own event emitter for the React warning and performance when using 1000+ useContextSelectors. This also gave some size advantages for me, I’ll answer with how I used it here tomorrow.

0reactions
dai-shicommented, Jan 17, 2020

Closing this. Please open a new issue for further discussion.

Read more comments on GitHub >

github_iconTop Results From Across the Web

14 Examples of Documentation Mistakes You Are Making
When it comes to doing something right, it sometimes helps to see how it's done wrong. So here are 13 examples of bad...
Read more >
Eight Common Database Design Bad Practices - Toptal
In this article, Toptal Freelance Software Engineer Fernando Martinez discusses some of the most common database design bad practices and how to avoid...
Read more >
Process Documentation: Definition & Best Practices - Helpjuice
Process documentation maps out an ideal way of completing a workflow in your business. Here's everything you need to know and how to...
Read more >
Putting comments in code: the good, the bad, and the ugly.
“Good code is self-documenting.” In 20+ years of writing code for a living, this is the one phrase I've heard the most. It's...
Read more >
Best practices for writing code comments - Stack Overflow Blog
The most infamous comment in the Unix source code is “You are not ... above examples have shown that comments don't excuse or...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found