Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor README changes #1

Merged
merged 2 commits into from
Aug 12, 2020
Merged

Minor README changes #1

merged 2 commits into from
Aug 12, 2020

Conversation

gsimone
Copy link
Member

@gsimone gsimone commented Aug 12, 2020

I tried to make the README more new user friendly.

A couple of questions:

const multiplyCountAtom = create({
  write: ({ get, set }, multiplicator) => set(countAtom, get(countAtom) * multiplicator),
})

can this be written like

const multiplyCountAtom = create({
  write: ({ get, set }, multiplicator) => {
     set(countAtom, get(countAtom) * multiplicator)
 },
})

Or does it need the return value of set() ?


Same here, can it be rewritten with {} to make it less cryptic?

import { create } from 'jotai'
import countAtom from './countAtom.js'

const multiplyCountAtom = create({
  write: ({ get, set }, multiplicator) => {
     set(countAtom, get(countAtom) * multiplicator)
  }
})

function Controls() {
  const [, multiply] = useAtom(decrementCountAtom)
  return <button onClick={() => multiply(3)}>triple</button>
}

TLDR I would be careful with the short syntax for arrow functions because it can imply something that isn't necessary ( this is strictly related to the README )

@gsimone gsimone requested a review from dai-shi August 12, 2020 15:35
@stephencorwin
Copy link
Member

stephencorwin commented Aug 12, 2020

Same here, can it be rewritten with {} to make it less cryptic

In my opinion, arrow syntax makes it easier to read partially because it takes less space.
Given the example:

// easy to read
<button onClick={() => multiply(3)}>triple</button>

// more difficult to read
<button
  onClick={() => {
    multiply(3)
  }}
>
  triple
</button>

// most difficult to read
<button onClick={() => {multiply(3)}}>triple</button>

This becomes exaggerated when there are many properties on a component

// easy to read
<button
  onClick={() => multiply(3)}
  onContextMenu={() => multiply(3)}
  color="blue"
>
  triple
</button>

// more difficult to read
<button
  onClick={() => {multiply(3)}}
  onContextMenu={() => {multiply(3)}}
  color="blue"
>
  triple
</button>

// most difficult to read
<button
  onClick={() => {
    multiply(3)
  }}
  onContextMenu={() => {
    multiply(3)
  }}
  color="blue"
>
  triple
</button>

TLDR I would be careful with the short syntax for arrow functions because it can imply something that isn't necessary

While I understand the sentiment, I think that the readability advantages outweigh implications. In general, functions should be tolerant to arrow function implicit return.

One that is not tolerant which causes confusion is useEffect

// error
useEffect(() => multiply(3))

// success
useEffect(() => {
  multiply(3)
})

// success
useEffect(() => {
  multiply(3)
  // cleanup function
  return () => {}
})

This intolerance causes many bugs and in my opinion is frustrating to orchestrate because of it.

To sum it up

This topic is highly opinionated, but I think that generally speaking, we should be leveraging implicitly returned arrow functions because it nets less LOC which is one of the determining factors in how complex something is perceived to be.

// less perceived complexity
const myFunction = (a, b) => a + b

// more perceived complexity
function MyFunction(a, b) {
  return a + b
}

@gsimone
Copy link
Member Author

gsimone commented Aug 12, 2020

I wouldn't use it for callbacks ( user is supposed to know what those do, and if they don't it's react's docs job ) or effects ( same )

EDIT: As you can see this is the case in the examples, I left the unambiguos calls in the short form (like onClick)

@dai-shi
Copy link
Member

dai-shi commented Aug 12, 2020

Thanks for the review and the change suggestion.

wrt set, it returns undefined and arrow functions are not necessary.
I understand the both opinions. Personally, () => { foo(); } is acceptable, but () => foo(); is often more used, I guess.

Merging this.

@dai-shi dai-shi merged commit d4b35d2 into master Aug 12, 2020
@dai-shi dai-shi deleted the gsimone-readme-changes branch August 12, 2020 21:42
@dai-shi
Copy link
Member

dai-shi commented Aug 12, 2020

On second thought, I think I would change write to non-arrow. It doesn't suffer from readability unlike props in JSX. f149664

dai-shi pushed a commit that referenced this pull request Feb 13, 2021
* Update readme.md

* Restyled by prettier-markdown

Co-authored-by: Restyled.io <[email protected]>

Co-authored-by: Restyled.io <[email protected]>
dai-shi pushed a commit that referenced this pull request May 8, 2021
dai-shi added a commit that referenced this pull request May 31, 2022
* fix typo

* rework async part

* added introduction

* common preload for async read

* re-read #1

* prettier & remove async old statement

* added loadable utils

* re-ran prettier

* cleaned loadable

* improve loadable intro

* quick update

* fixed write async atoms suspend timing

* moved async

* Update docs/guides/persistence.mdx

Co-authored-by: Daishi Kato <[email protected]>

* Update docs/guides/async.mdx

Co-authored-by: Daishi Kato <[email protected]>

* simplify Suspense

* Update docs/guides/async.mdx

Co-authored-by: Daishi Kato <[email protected]>

* revert readme

Co-authored-by: Daishi Kato <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants