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

[Docs] Replace useState with edit in useEntityRecord usage examples #43270

Merged
merged 4 commits into from Aug 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 9 additions & 4 deletions packages/core-data/README.md
Expand Up @@ -765,26 +765,31 @@ application, the page and the resolution details will be retrieved from
the store state using `getEntityRecord()`, or resolved if missing.

```js
import { useState } from '@wordpress/data';
import { useDispatch } from '@wordpress/data';
import { useCallback } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { TextControl } from '@wordpress/components';
import { store as noticeStore } from '@wordpress/notices';
import { useEntityRecord } from '@wordpress/core-data';

function PageRenameForm( { id } ) {
const page = useEntityRecord( 'postType', 'page', id );
const [ title, setTitle ] = useState( () => page.record.title.rendered );
const { createSuccessNotice, createErrorNotice } =
useDispatch( noticeStore );

const setTitle = useCallback(
( title ) => {
page.edit( { title } );
},
[ page.edit ]
);

if ( page.isResolving ) {
return 'Loading...';
}

async function onRename( event ) {
event.preventDefault();
page.edit( { title } );
try {
await page.save();
createSuccessNotice( __( 'Page renamed.' ), {
Expand All @@ -799,7 +804,7 @@ function PageRenameForm( { id } ) {
<form onSubmit={ onRename }>
<TextControl
label={ __( 'Name' ) }
value={ title }
value={ page.editedRecord.title }
onChange={ setTitle }
/>
<button type="submit">{ __( 'Save' ) }</button>
Expand Down
10 changes: 6 additions & 4 deletions packages/core-data/src/hooks/use-entity-record.ts
Expand Up @@ -84,26 +84,28 @@ export interface Options {
*
* @example
* ```js
* import { useState } from '@wordpress/data';
* import { useDispatch } from '@wordpress/data';
* import { useCallback } from '@wordpress/element';
* import { __ } from '@wordpress/i18n';
* import { TextControl } from '@wordpress/components';
* import { store as noticeStore } from '@wordpress/notices';
* import { useEntityRecord } from '@wordpress/core-data';
*
* function PageRenameForm( { id } ) {
* const page = useEntityRecord( 'postType', 'page', id );
* const [ title, setTitle ] = useState( () => page.record.title.rendered );
* const { createSuccessNotice, createErrorNotice } =
* useDispatch( noticeStore );
*
* const setTitle = useCallback( ( title ) => {
* page.edit( { title } );
* }, [ page.edit ] );
*
* if ( page.isResolving ) {
* return 'Loading...';
* }
*
* async function onRename( event ) {
* event.preventDefault();
* page.edit( { title } );
* try {
* await page.save();
* createSuccessNotice( __( 'Page renamed.' ), {
Expand All @@ -118,7 +120,7 @@ export interface Options {
* <form onSubmit={ onRename }>
* <TextControl
* label={ __( 'Name' ) }
* value={ title }
* value={ page.editedRecord.title }
* onChange={ setTitle }
* />
* <button type="submit">{ __( 'Save' ) }</button>
Expand Down