Skip to content

[Rough Draft] Composition-API: Introduction #67

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

Merged
merged 6 commits into from
May 3, 2020

Conversation

shentao
Copy link
Member

@shentao shentao commented Apr 14, 2020

This PR is a draft of the Composition API: Introduction Tutorial.

Here's what the PR includes:

  • Why would you want to use Composition API?
  • Intro to setup option
  • Intro to ref (`reactive is omitted in the introduction tutorial)
  • Lifecycle hooks registration inside setup
  • Intro to watch (watchEffect is omitted in the introduction tutorial)
  • Intro to computed
  • Extracting code into separate function/files

What's left to do:

  • Build codepens (or codesandboxes for multi-file projects?) if we consider them necessary.
  • The Notion draft included color highlighting for the code snippets which I think help with understanding what goes where during the refactor. See: https://www.notion.so/shentao/Composition-API-Introduction-e86b4fe667a5416bb8ee87542e168f0b
  • Minor styling fixes to the output exported from Notion (images, headers)
  • Decide if omitting reactive and watchEffect is a good idea. I believe it is and here’s my reasoning:
    • Regarding reactive – I don’t think we need to show two ways of doing the same thing at this stage, where I think the most important part is to show how the Option API can translate to Composition API. Also introducing reactive at this point could confuse the reader and is by no means justified by the code itself.
    • Similarly watchEffect doesn’t really have a counterpart in the options API (unless we consider how the render function is working 😅). Thus explaining it here would unnecessarily make the text longer and could sidetrack the reader by showing something that again is not necessary from the tutorial perspective.

Open to feedback! I think it might still be a bit too long...

Edit: Path to preview is /guide/composition-api-introduction.html.

Copy link
Member

@phanan phanan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work! I've added a couple of wording suggestions, mainly to eliminate duplicates and filling words/phrases. You'll notice that in most cases I suggested a simpler and/or shorter expression (at least IMHO) :)

Copy link
Member Author

@shentao shentao left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @phanan for reading through it and providing suggestions! Loved all of them and already fixed the ones where it could be optimized.


![Composition%20API%20Introduction/62783021-7ce24400-ba89-11e9-9dd3-36f4f6b1fae2.png](Composition%20API%20Introduction/62783021-7ce24400-ba89-11e9-9dd3-36f4f6b1fae2.png)

Please consider this colourful example on the side for second or two as it presents a larger component with its **logical concerns** grouped by colours.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was supposed to provide some context to the image. Might skip that "colorful" part.

Comment on lines +114 to +115
repositories,
getUserRepositories // functions returned behave the same as methods
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah – leftover from Notion. Will have to convert in the editor! Good catch.

@NataliaTepluhina
Copy link
Member

Great work @shentao! As I was doing a (p)review of the Notion doc, I don't have any additional comments 😅

@shentao
Copy link
Member Author

shentao commented Apr 14, 2020

Thanks @NataliaTepluhina! Let me know if there is any other way I could help out!

Copy link
Member

@znck znck left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very long PR, I will continue reviewing it.


Organizing logics with component's options (`data`, `computed`, `methods`, `watch`) works in most cases. However, when our components get bigger, the list of **logical concerns** also grows. This can lead to components that are hard to read and understand, especially for people who didn't write them in the first place.

![Vue Option API: Code grouped by option type](https://user-images.githubusercontent.com/499550/62783021-7ce24400-ba89-11e9-9dd3-36f4f6b1fae2.png)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This image appears out of context to me. I feel it would be better if we reuse the above snippet covey the intension. And a better resolution would be great.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call @znck! I just picked the image from the RFC page, since it explained the idea pretty nicely in a visual manner. I used the same thing a few times during talks and it was the part that proved to be very educative to the audience. What about using the second image where it shows the two files (pre and post composition API)?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No doubt the image is good visual representation of logic fragmentation. I meant that we can use the above code snippet and highlight in a similar manner. Since readers have already read the code above, it would be easier for them to scan the coloured highlights.


![Composition%20API%20Introduction/62783021-7ce24400-ba89-11e9-9dd3-36f4f6b1fae2.png](Composition%20API%20Introduction/62783021-7ce24400-ba89-11e9-9dd3-36f4f6b1fae2.png)

Please consider this colourful example on the side for second or two as it presents a larger component with its **logical concerns** grouped by colours.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, please.

suggestion: "logical concerns grouped by colours" should be the key focus of the paragraph and less dramatic text would be better to match overall writing style.


Now that we know the **why** we can get to the **how**. To start working with the Compsition API we first need a place where we can actually use it. In a Vue component, we call this place the `setup`.

### Introducing `setup`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: The heading feels more like from an article. Can we reword it (without Introduction maybe)?

@phanan
Copy link
Member

phanan commented Apr 15, 2020 via email

Copy link
Member

@phanan phanan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, solid work @shentao! I'm using some free time before work to add a couple more super minor suggestions (some might even be the result of my last review!). My general idea is to keep the documentation as short and straightforward as it could be by:

  • Merging/removing sentences/phrases that convey the same/similar message.
  • Reducing the usage of filler and/or assuming phrases such as "As you know" or "You might have noticed." While these phrases add a friendly touch to the document, overusing them might lengthen (and dilute) the doc, cause a dramatic feel, and is prone to struggle invalidation.


Now we need to react to the changes made to the `user` prop. For that we will use the standalone `watch` function.

### Introducing standalone `watch`
Copy link
Member

@phanan phanan Apr 15, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The headings should have the same case (currently it's Title Case e.g., Ready for More?).

@znck
Copy link
Member

znck commented Apr 15, 2020

@shentao You can use grammarly.com to catch issues like "color" instead of "colour" and there's a vscode extension too (shameless plug).

Co-Authored-By: Phan An <[email protected]>
Co-Authored-By: Rahul Kadyan <[email protected]>
@shentao
Copy link
Member Author

shentao commented Apr 15, 2020

Thanks for the reviews @znck and @phanan (again!). Yeah, I do use Grammarly quite often. I think my system is just set to British English (gotta change that).

I’m not using VS Code!

@NataliaTepluhina NataliaTepluhina added the enhancement New feature or request label Apr 15, 2020
@NataliaTepluhina NataliaTepluhina added this to the alpha milestone Apr 15, 2020
@NataliaTepluhina
Copy link
Member

As we have no further comments, I'm merging this PR 🎉

@NataliaTepluhina NataliaTepluhina merged commit b31cbfc into master May 3, 2020
NataliaTepluhina added a commit that referenced this pull request May 4, 2020
commit 5509287
Author: Natalia Tepluhina <[email protected]>
Date:   Mon May 4 06:57:13 2020 +0300

    Options API Reference (#73)

    * feat: started with Global Config API

    * feat: finished global config

    * feat: added component global api

    * feat: added directives API

    * feat: added mixin API

    * feat: add mount API

    * feat: added unmount and use

    * Update src/api/global-api.md

    Co-Authored-By: Jinjiang <[email protected]>

    * Update src/api/global-config.md

    Co-Authored-By: Jinjiang <[email protected]>

    * fixL changed global -> application

    * fix: fixed types for config

    * fix: fixed types for API

    * feat: added a note about native HTML tags

    * feat: added options data chapter

    * feat: finished options data

    * feat: added options-DOM

    * Added emits to data

    * feat: added lifecycle hooks

    * feat: added renderTracked and renderTriggered

    * feat: added options assets

    * feat: added options composition

    * feat: added misc to options

    * fix: TODO cleanup

    * fix: changed data description

    Co-authored-by: Jinjiang <[email protected]>

commit b31cbfc
Author: Damian Dulisz <[email protected]>
Date:   Sun May 3 16:18:35 2020 +0200

    [Rough Draft] Composition-API: Introduction (#67)

    * Add Composition-API: Introduction draft

    * Fix image links

    * Add styling for Tips and Warnings. Fix missing `js` syntax flag

    * Apply suggestions from An Phan

    Co-Authored-By: Phan An <[email protected]>

    * Apply suggestions from code review

    Co-Authored-By: Phan An <[email protected]>
    Co-Authored-By: Rahul Kadyan <[email protected]>

    * Convert tabs to spaces

    Co-authored-by: Phan An <[email protected]>
    Co-authored-by: Rahul Kadyan <[email protected]>

commit 19c7285
Author: NataliaTepluhina <[email protected]>
Date:   Sun May 3 17:18:17 2020 +0300

    fix: fixed slots inconsistence
NataliaTepluhina added a commit that referenced this pull request May 4, 2020
* feat: started with Global Config API

* feat: finished global config

* feat: added component global api

* feat: added directives API

* feat: added mixin API

* feat: add mount API

* feat: added unmount and use

* Update src/api/global-api.md

Co-Authored-By: Jinjiang <[email protected]>

* Update src/api/global-config.md

Co-Authored-By: Jinjiang <[email protected]>

* fixL changed global -> application

* fix: fixed types for config

* fix: fixed types for API

* feat: added a note about native HTML tags

* feat: added options data chapter

* feat: finished options data

* feat: added options-DOM

* Added emits to data

* feat: added lifecycle hooks

* feat: added renderTracked and renderTriggered

* feat: added options assets

* feat: added options composition

* feat: added misc to options

* fix: TODO cleanup

* fix: changed data description

* Added instance properties

* Squashed commit of the following:

commit 5509287
Author: Natalia Tepluhina <[email protected]>
Date:   Mon May 4 06:57:13 2020 +0300

    Options API Reference (#73)

    * feat: started with Global Config API

    * feat: finished global config

    * feat: added component global api

    * feat: added directives API

    * feat: added mixin API

    * feat: add mount API

    * feat: added unmount and use

    * Update src/api/global-api.md

    Co-Authored-By: Jinjiang <[email protected]>

    * Update src/api/global-config.md

    Co-Authored-By: Jinjiang <[email protected]>

    * fixL changed global -> application

    * fix: fixed types for config

    * fix: fixed types for API

    * feat: added a note about native HTML tags

    * feat: added options data chapter

    * feat: finished options data

    * feat: added options-DOM

    * Added emits to data

    * feat: added lifecycle hooks

    * feat: added renderTracked and renderTriggered

    * feat: added options assets

    * feat: added options composition

    * feat: added misc to options

    * fix: TODO cleanup

    * fix: changed data description

    Co-authored-by: Jinjiang <[email protected]>

commit b31cbfc
Author: Damian Dulisz <[email protected]>
Date:   Sun May 3 16:18:35 2020 +0200

    [Rough Draft] Composition-API: Introduction (#67)

    * Add Composition-API: Introduction draft

    * Fix image links

    * Add styling for Tips and Warnings. Fix missing `js` syntax flag

    * Apply suggestions from An Phan

    Co-Authored-By: Phan An <[email protected]>

    * Apply suggestions from code review

    Co-Authored-By: Phan An <[email protected]>
    Co-Authored-By: Rahul Kadyan <[email protected]>

    * Convert tabs to spaces

    Co-authored-by: Phan An <[email protected]>
    Co-authored-by: Rahul Kadyan <[email protected]>

commit 19c7285
Author: NataliaTepluhina <[email protected]>
Date:   Sun May 3 17:18:17 2020 +0300

    fix: fixed slots inconsistence

* Revert "Squashed commit of the following:"

This reverts commit b62c049.

Co-authored-by: Jinjiang <[email protected]>
@NataliaTepluhina NataliaTepluhina deleted the composition-api-intro branch July 17, 2020 14:40
TalexDreamSoul pushed a commit to Talexs/docs that referenced this pull request Apr 17, 2022
…uejs#67)

* docs(event-handling): review src/guide/essentials/event-handling.md

* Apply suggestions from code review

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

Successfully merging this pull request may close these issues.

4 participants