Repository navigation
Translate set-state-in-render lint rule #999
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
Open
smikitky
wants to merge
1
commit into
main
Choose a base branch
from
tr/eslint-set-state-in-render
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -4,17 +4,17 @@ title: set-state-in-render | |
|
|
||
| <Intro> | ||
|
|
||
| Validates against unconditionally setting state during render, which can trigger additional renders and potential infinite render loops. | ||
| 余分なレンダーや無限レンダーループにつながる可能性のある、レンダー中の無条件な state 更新を行っていないか検証します。 | ||
|
|
||
| </Intro> | ||
|
|
||
| ## Rule Details {/*rule-details*/} | ||
| ## ルールの詳細 {/*rule-details*/} | ||
|
|
||
| Calling `setState` during render unconditionally triggers another render before the current one finishes. This creates an infinite loop that crashes your app. | ||
| レンダー中に無条件で `setState` を呼び出すと、現在のレンダーが終わる前に新しいレンダーがトリガされます。これにより、無限ループが発生し、アプリがクラッシュします。 | ||
|
|
||
| ## Common Violations {/*common-violations*/} | ||
| ## よくある違反 {/*common-violations*/} | ||
|
|
||
| ### Invalid {/*invalid*/} | ||
| ### 無効な例 {/*invalid*/} | ||
|
|
||
| ```js {expectedErrors: {'react-compiler': [4]}} | ||
| // ❌ Unconditional setState directly in render | ||
|
|
@@ -25,7 +25,7 @@ function Component({value}) { | |
| } | ||
| ``` | ||
|
|
||
| ### Valid {/*valid*/} | ||
| ### 有効な例 {/*valid*/} | ||
|
|
||
| ```js | ||
| // ✅ Derive during render | ||
|
|
@@ -65,11 +65,11 @@ function Component({ items }) { | |
| } | ||
| ``` | ||
|
|
||
| ## Troubleshooting {/*troubleshooting*/} | ||
| ## トラブルシューティング {/*troubleshooting*/} | ||
|
|
||
| ### I want to sync state to a prop {/*clamp-state-to-prop*/} | ||
| ### state を props と同期したい {/*clamp-state-to-prop*/} | ||
|
|
||
| A common problem is trying to "fix" state after it renders. Suppose you want to keep a counter from exceeding a `max` prop: | ||
| よくある問題は、レンダーした後で state を「修正」しようとすることです。例えば、以下のようにカウンタが `max` プロパティの値を超えないようにしたいとします。 | ||
|
|
||
| ```js | ||
| // ❌ Wrong: clamps during render | ||
|
|
@@ -88,9 +88,9 @@ function Counter({max}) { | |
| } | ||
| ``` | ||
|
|
||
| As soon as `count` exceeds `max`, an infinite loop is triggered. | ||
| `count` が `max` を超えると、すぐに無限ループが発生します。 | ||
|
|
||
| Instead, it's often better to move this logic to the event (the place where the state is first set). For example, you can enforce the maximum at the moment you update state: | ||
| 代わりに、このロジックをイベント(最初に state を設定する場所)へ移す方が良い場合が多いです。例えば、state を更新する時点で、上限を超えないようにできます。 | ||
|
|
||
| ```js | ||
| // ✅ Clamp when updating | ||
|
|
@@ -105,6 +105,6 @@ function Counter({max}) { | |
| } | ||
| ``` | ||
|
|
||
| Now the setter only runs in response to the click, React finishes the render normally, and `count` never crosses `max`. | ||
| これで、セッタはクリックに応じてのみ実行され、React はレンダーを正常に完了し、`count` が `max` を超えることはなくなります。 | ||
|
|
||
| In rare cases, you may need to adjust state based on information from previous renders. For those, follow [this pattern](https://react.dev/reference/react/useState#storing-information-from-previous-renders) of setting state conditionally. | ||
| まれに、以前のレンダーの情報に基づいて state を調整する必要があるかもしれません。その場合は、条件付きで state を設定する[こちらのパターン](https://react.dev/reference/react/useState#storing-information-from-previous-renders)に従ってください。 | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. このドメイン付きURLは原文にPRを出して修正予定です。 |
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
これは正しい指摘なのですが原文の問題なので原文にPRを出して修正予定です。