JavaScript Code Style
ScandiPWA follows a strict JavaScript style guide for maintainability and consistency
Writing Maintainable Code
Keep Functions Short
renderCreateAccount() {
const {
state,
onCreateAccountAttempt,
onCreateAccountSuccess,
handleSignIn
} = this.props;
return (
<>
<Form
key="create-account"
onSubmit={ onCreateAccountAttempt }
onSubmitSuccess={ onCreateAccountSuccess }
onSubmitError={ onCreateAccountAttempt }
>
<fieldset block="MyAccountOverlay" elem="Legend">
<legend>{ __('Personal Information') }</legend>
<Field
type="text"
label={ __('First Name') }
id="firstname"
name="firstname"
autocomplete="given-name"
validation={ ['notEmpty'] }
/>
<Field
type="text"
label={ __('Last Name') }
id="lastname"
name="lastname"
autocomplete="family-name"
validation={ ['notEmpty'] }
/>
<Field
type="checkbox"
value="is_subscribed"
label={ __('Subscribe to newsletter') }
id="is_subscribed"
mix={ { block: 'MyAccountOverlay', elem: 'Checkbox' } }
name="is_subscribed"
/>
</fieldset>
<fieldset block="MyAccountOverlay" elem="Legend">
<legend>{ __('Sign-Up Information') }</legend>
<Field
type="text"
label={ __('Email') }
id="email"
name="email"
autocomplete="email"
validation={ ['notEmpty', 'email'] }
/>
<Field
type="password"
label={ __('Password') }
id="password"
name="password"
autocomplete="new-password"
validation={ ['notEmpty', 'password'] }
/>
<Field
type="password"
label={ __('Confirm password') }
id="confirm_password"
name="confirm_password"
autocomplete="new-password"
validation={ ['notEmpty', 'password', 'password_match'] }
/>
</fieldset>
<div block="MyAccountOverlay" elem="Buttons">
<button
block="Button"
type="submit"
>
{ __('Sign up') }
</button>
</div>
</Form>
<article block="MyAccountOverlay" elem="Additional" mods={ { state } }>
<section>
<h4>{ __('Already have an account?') }</h4>
<button
block="Button"
mods={ { likeLink: true } }
onClick={ handleSignIn }
>
{ __('Sign in here') }
</button>
</section>
</article>
</>
);
}Separate Concerns
Use Destructuring
Use Meaningful Names
Avoid Magic Numbers
Functional Programming
Avoid let; Use const
let; Use constAvoid Loops
Working with Arrays
Creating a new array by transforming each item
Reducing the array to 1 value
Copying part of the array
Finding an item in the array
Reordering the array
Adding an item to the array
Removing an item from an array
Extensibility Best Practices
Export Everything
Add Namespaces
ScandiPWA Conventions
Follow the File Structure
One Class Per File
Last updated