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

[Documentation] Add examples for all actions/selectors for the @wordpress/notice package. #42077

Conversation

ryanwelcher
Copy link
Contributor

What?

This PR introduces practical examples for the notice package documentation. Part of the effort in WordPress/Documentation-Issue-Tracker#379

Why?

WIthout examples, the data store APIs can be very confusing.

ryanwelcher and others added 24 commits March 24, 2022 11:23
@ryanwelcher ryanwelcher added [Type] Developer Documentation Documentation for developers [Package] Notices /packages/notices Developer Experience Ideas about improving block and theme developer experience labels Jun 30, 2022
@ryanwelcher ryanwelcher changed the title Feature/add examples to notices package actions [Documentation] Add examples for all actions/selectors for the @wordpress/notice package. Jul 1, 2022
@ryanwelcher
Copy link
Contributor Author

Tracking ticket for this effort is here - #42125

<Button
onClick={ () => createNotice( 'success', __( 'Notice message' ) ) }
>
{ __( 'Generate a success notice!' ) }
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be nice to include all the required imports in this example, so useDispatch and __. Also, the preferred way of calling useDispatch is by passing the imported store object as follows:

import { store as noticesStore } from '@wordpress/notices';
// ...
const { createNotice } = useDispatch( noticesStore );

Copy link
Contributor

Choose a reason for hiding this comment

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

(I mean that for all the examples)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks! I love importing the store name suggestion. All examples are updated.

@ryanwelcher ryanwelcher requested a review from adamziel July 4, 2022 16:28
@gziolo
Copy link
Member

gziolo commented Jul 5, 2022

All the examples present how to use actions/selectors from the React component. In practice, it doesn't necessarily need to be the case. It's possible to use also the following snippet:

import { select } from '@wordpress/data';
import { store as noticesStore } from '@wordpress/notices';

console.log( select( noticesStore ).getNotices() );

I'm not sure whether it's something we should include in the examples, but we definitely should make it clear that the usage depends on the context.

@ryanwelcher
Copy link
Contributor Author

I'm not sure whether it's something we should include in the examples, but we definitely should make it clear that the usage depends on the context.

It's certainly possible to use the selectors this way, and we can also access and use them by calling wp.data.select('core/notices).getNotices() but, in my opinion, showing all the ways is just going to be more confusing.

I think the majority of cases will be either in a block's Edit component or in a SlotFill which will also be a component. So I think it make sense to show examples for the most common usage. Is there a common use-case I am not considering?

@ryanwelcher ryanwelcher requested a review from gziolo July 5, 2022 16:13
Copy link
Member

@gziolo gziolo left a comment

Choose a reason for hiding this comment

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

Excellent work add all those examples.

@gziolo
Copy link
Member

gziolo commented Jul 6, 2022

I think the majority of cases will be either in a block's Edit component or in a SlotFill which will also be a component. So I think it make sense to show examples for the most common usage. Is there a common use-case I am not considering?

I agree with that. That seems like the most common use case. On the flip side, it gives a false impression that you need React to use data stores. It's something we might want to emphasize in the documentation for @wordpress/data.

@ryanwelcher ryanwelcher merged commit af7da80 into WordPress:trunk Jul 6, 2022
@github-actions github-actions bot added this to the Gutenberg 13.7 milestone Jul 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Developer Experience Ideas about improving block and theme developer experience [Package] Notices /packages/notices [Type] Developer Documentation Documentation for developers
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants