-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Labels
enhancementNew feature or requestNew feature or request
Description
More feedback from @luisherranz
Avoid storing state that can be derived from other state.
const { state } = store( "...", {
state: {
get hasSucess() {
const { status } = getContext();
return status === "success";
},
get hasError() {
const { status } = getContext();
return status === "error";
},
get formMessage() {
const { status } = getContext();
if ( status === "success" ) return "Success!";
if ( status === "error" ) return "Error!";
}
},
actions: {
*submitForm( event ) {
// ...
try {
yield fetch( context.ajaxUrl, {
method: 'POST',
credentials: 'same-origin',
body: formData
}).then( r => r.text() )
context.status = "success";
} catch( error ) {
console.error( 'Error:', error );
context.status = "error";
}
},
}
});This keeps things more declarative and avoids out-of-sync issues when the code starts getting complex.
I hope that helps 🙂
Metadata
Metadata
Assignees
Labels
enhancementNew feature or requestNew feature or request