-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
83 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# eslint-plugin-warp-drive | ||
|
||
## Rules | ||
|
||
- [no-create-record-rerender](./no-create-record-rerender.md) |
75 changes: 75 additions & 0 deletions
75
packages/eslint-plugin-ember-data/docs/no-create-record-rerender.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# eslint-plugin-warp-drive | ||
|
||
## no-create-record-rerender | ||
|
||
Helps avoid patterns that often lead to excess or broken renders. | ||
|
||
## Don't use `createRecord` inside constructors, getters, or class properties | ||
|
||
Calling `createRecord` in these places can cause unexpected re-renders and may blow up in EmberData 4.12+. | ||
|
||
### Incorrect Code | ||
|
||
```gjs | ||
import Component from '@glimmer/component'; | ||
import { tracked } from '@glimmer/tracking'; | ||
import { on } from '@ember/modifier'; | ||
class MyForm extends Component { | ||
@service store; | ||
// ERROR: Cannot call `store.createRecord` in a class property initializer. | ||
// Calling `store.createRecord` inside constructors, getters, and class | ||
// properties can cause issues with re-renders. | ||
model = this.store.createRecord('user'); | ||
<template> | ||
{{!-- Some Template !--}} | ||
</template> | ||
} | ||
export default ParentComponent extends Component { | ||
@tracked isShowingForm = false; | ||
@action rerenderWithForm() { | ||
this.isShowingForm = true; | ||
} | ||
<template> | ||
{{#if this.isShowingForm}} | ||
<MyForm /> | ||
{{/if}} | ||
<button type="button" {{on "click" this.rerenderWithForm}}>Show the form</button> | ||
</template> | ||
} | ||
``` | ||
|
||
### Correct Code | ||
|
||
```gjs | ||
// app/components/parent-component.gts | ||
import Component from '@glimmer/component'; | ||
import { tracked } from '@glimmer/tracking'; | ||
import { on } from '@ember/modifier'; | ||
class MyForm extends Component { | ||
<template> | ||
{{!-- Some Template !--}} | ||
</template> | ||
} | ||
export default class ParentComponent extends Component { | ||
@tracked isShowingForm = false; | ||
rerenderForm = () => { | ||
this.model = this.store.createRecord('user'); | ||
this.isShowingForm = true; | ||
} | ||
<template> | ||
{{#if this.isShowingForm}} | ||
<MyForm @model={{this.model}} /> | ||
{{/if}} | ||
<button type="button" {{on "click" this.rerenderForm}}>Show the child component</button> | ||
</template> | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ | |
}, | ||
"files": [ | ||
"src", | ||
"docs", | ||
"CHANGELOG.md", | ||
"README.md" | ||
], | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters