Skip to content

Commit

Permalink
Docs: Update sample code to fix React warning error on Tutorial page (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
t-hamano committed Dec 28, 2023
1 parent d7c222c commit e41b9f1
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions docs/getting-started/tutorial.md
Expand Up @@ -483,7 +483,7 @@ export default function Edit( { attributes, setAttributes } ) {
'Starting year',
'copyright-date-block'
) }
value={ startingYear }
value={ startingYear || '' }
onChange={ ( value ) =>
setAttributes( { startingYear: value } )
}
Expand All @@ -496,6 +496,10 @@ export default function Edit( { attributes, setAttributes } ) {
}
```
<div class="callout callout-tip">
You may have noticed that the <code>value</code> property has a value of <code>startingYear || ''</code>. The symbol <code>||</code> is called the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_OR">Logical OR</a> (logical disjunction) operator. This prevents warnings in React when the <code>startingYear</code> is empty. See <a href="https://react.dev/learn/sharing-state-between-components#controlled-and-uncontrolled-components">Controlled and uncontrolled components</a> for details.
</div>
Save the file and refresh the Editor. Confirm that a text field now exists in the Settings panel. Add a starting year and confirm that when you update the page, the value is saved.
![A live look at editing the new Starting Year field in the Settings Sidebar](https://developer.wordpress.org/files/2023/12/block-tutorial-11.gif)
Expand All @@ -522,7 +526,7 @@ export default function Edit( { attributes, setAttributes } ) {
<InspectorControls>
<PanelBody title={ __( 'Settings', 'copyright-date-block' ) }>
<ToggleControl
checked={ showStartingYear }
checked={ !! showStartingYear }
label={ __(
'Show starting year',
'copyright-date-block'
Expand All @@ -539,7 +543,7 @@ export default function Edit( { attributes, setAttributes } ) {
'Starting year',
'copyright-date-block'
) }
value={ startingYear }
value={ startingYear || '' }
onChange={ ( value ) =>
setAttributes( { startingYear: value } )
}
Expand Down Expand Up @@ -601,7 +605,7 @@ export default function Edit( { attributes, setAttributes } ) {
<InspectorControls>
<PanelBody title={ __( 'Settings', 'copyright-date-block' ) }>
<ToggleControl
checked={ showStartingYear }
checked={ !! showStartingYear }
label={ __(
'Show starting year',
'copyright-date-block'
Expand All @@ -618,7 +622,7 @@ export default function Edit( { attributes, setAttributes } ) {
'Starting year',
'copyright-date-block'
) }
value={ startingYear }
value={ startingYear || '' }
onChange={ ( value ) =>
setAttributes( { startingYear: value } )
}
Expand Down

0 comments on commit e41b9f1

Please sign in to comment.