Skip to content

Commit

Permalink
chore(linter/react_perf): improve docs for react_perf rules (#5481)
Browse files Browse the repository at this point in the history
Please fill in the "why this is bad" section

_Originally posted by @DonIsaac in
#5360 (comment)
  • Loading branch information
shulaoda committed Sep 5, 2024
1 parent bfab091 commit bc7be70
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 14 deletions.
17 changes: 14 additions & 3 deletions crates/oxc_linter/src/rules/react_perf/jsx_no_jsx_as_prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,27 @@ pub struct JsxNoJsxAsProp;
declare_oxc_lint!(
/// ### What it does
///
/// Prevent JSX that are local to the current method from being used as values of JSX props
/// Prevent JSX elements that are local to the current method from being
/// used as values of JSX props.
///
/// ### Why is this bad?
///
/// Using locally defined JSX elements as values for props can lead to
/// unintentional re-renders and performance issues. Every time the parent
/// renders, a new instance of the JSX element is created, causing unnecessary
/// re-renders of child components. This also leads to harder-to-maintain code
/// as the component's props are not passed consistently.
///
/// ### Example
/// Examples of **incorrect** code for this rule:
/// ```jsx
/// // Bad
/// <Item jsx={<SubItem />} />
/// <Item jsx={this.props.jsx || <SubItem />} />
/// <Item jsx={this.props.jsx ? this.props.jsx : <SubItem />} />
/// ```
///
/// // Good
/// Examples of **correct** code for this rule:
/// ```jsx
/// <Item callback={this.props.jsx} />
/// ```
JsxNoJsxAsProp,
Expand Down
19 changes: 15 additions & 4 deletions crates/oxc_linter/src/rules/react_perf/jsx_no_new_array_as_prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,30 @@ pub struct JsxNoNewArrayAsProp;
declare_oxc_lint!(
/// ### What it does
///
/// Prevent Arrays that are local to the current method from being used as values of JSX props
/// Prevent Arrays that are local to the current method from being used
/// as values of JSX props.
///
/// ### Why is this bad?
///
/// Using locally defined Arrays as values for props can lead to unintentional
/// re-renders and performance issues. Every time the parent component renders,
/// a new instance of the Array is created, causing unnecessary re-renders of
/// child components. This also leads to harder-to-maintain code as the
/// component's props are not passed consistently.
///
/// ### Example
///
/// Examples of **incorrect** code for this rule:
/// ```jsx
/// // Bad
/// <Item list={[]} />
///
/// <Item list={new Array()} />
/// <Item list={Array()} />
/// <Item list={this.props.list || []} />
/// <Item list={this.props.list ? this.props.list : []} />
/// ```
///
/// // Good
/// Examples of **correct** code for this rule:
/// ```jsx
/// <Item list={this.props.list} />
/// ```
JsxNoNewArrayAsProp,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,27 @@ pub struct JsxNoNewFunctionAsProp;
declare_oxc_lint!(
/// ### What it does
///
/// Prevent Functions that are local to the current method from being used as values of JSX props
/// Prevent Functions that are local to the current method from being used
/// as values of JSX props.
///
/// ### Why is this bad?
///
/// Using locally defined Functions as values for props can lead to unintentional
/// re-renders and performance issues. Every time the parent component renders,
/// a new instance of the Function is created, causing unnecessary re-renders
/// of child components. This also leads to harder-to-maintain code as the
/// component's props are not passed consistently.
///
/// ### Example
///
/// Examples of **incorrect** code for this rule:
/// ```jsx
/// // Bad
/// <Item callback={new Function(...)} />
/// <Item callback={this.props.callback || function() {}} />
/// ```
///
/// // Good
/// Examples of **correct** code for this rule:
/// ```jsx
/// <Item callback={this.props.callback} />
/// ```
JsxNoNewFunctionAsProp,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,31 @@ pub struct JsxNoNewObjectAsProp;
declare_oxc_lint!(
/// ### What it does
///
/// Prevent Objects that are local to the current method from being used as values of JSX props
/// Prevent Objects that are local to the current method from being used
/// as values of JSX props.
///
/// ### Why is this bad?
///
/// Using locally defined Objects as values for props can lead to unintentional
/// re-renders and performance issues. Every time the parent component renders,
/// a new instance of the Object is created, causing unnecessary re-renders of
/// child components. This also leads to harder-to-maintain code as the
/// component's props are not passed consistently.
///
/// ### Examples
///
/// Examples of **incorrect** code for this rule:
/// ```jsx
/// // Bad
/// <Item config={{}} />
/// <Item config={new Object()} />
/// <Item config={Object()} />
/// <Item config={this.props.config || {}} />
/// <Item config={this.props.config ? this.props.config : {}} />
// <div style={{display: 'none'}} />
/// <div style={{display: 'none'}} />
/// ```
///
/// // Good
/// Examples of **correct** code for this rule:
/// ```jsx
/// <Item config={staticConfig} />
/// ```
JsxNoNewObjectAsProp,
Expand Down

0 comments on commit bc7be70

Please sign in to comment.