Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 135 additions & 0 deletions .grit/patterns/js/react_legacy_component.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
---
title: Update React legacy component
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
title: Update React legacy component
title: Remove legacy React lifecycle methods

---

detected Legacy component lifecycle
- `componentWillMount` -> `componentDidMount`
- `componentWillReceiveProps` -> `componentDidUpdate`
- `componentWillUpdate` -> `componentDidUpdate`
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These aren't direct replacements, so ideally we have some logic to detect things like when getSnapshotBeforeUpdate should be used. See https://legacy.reactjs.org/docs/react-component.html#unsafe_componentwillupdate.


tags: #migration, #fix

```grit
engine marzano(0.1)
language js


any {
or {`componentWillReceiveProps`,`componentWillUpdate`} => `componentDidUpdate`,
`componentWillMount` => `componentDidMount`,
}
```

## with `componentWillReceiveProps`

```javascript
class Test1 extends React.Component {
state = {
value: '',
};
// ruleid: react-legacy-component
componentWillReceiveProps(nextProps) {
this.setState({ value: nextProps.value });
}
handleChange = (e) => {
this.setState({ value: e.target.value });
};
render() {
return <input value={this.state.value} onChange={this.handleChange} />;
}
}
```

```javascript
class Test1 extends React.Component {
state = {
value: '',
};
// ruleid: react-legacy-component
componentDidUpdate(nextProps) {
this.setState({ value: nextProps.value });
}
handleChange = (e) => {
this.setState({ value: e.target.value });
};
render() {
return <input value={this.state.value} onChange={this.handleChange} />;
}
}
```

## with `componentWillUpdate`

```javascript
class Test1 extends React.Component {
state = {
value: '',
};
// ruleid: react-legacy-component
componentWillUpdate(nextProps) {
this.setState({ value: nextProps.value });
}
handleChange = (e) => {
this.setState({ value: e.target.value });
};
render() {
return <input value={this.state.value} onChange={this.handleChange} />;
}
}
```

```javascript
class Test1 extends React.Component {
state = {
value: '',
};
// ruleid: react-legacy-component
componentDidUpdate(nextProps) {
this.setState({ value: nextProps.value });
}
handleChange = (e) => {
this.setState({ value: e.target.value });
};
render() {
return <input value={this.state.value} onChange={this.handleChange} />;
}
}
```

## with `componentWillMount`

```javascript
class Test1 extends React.Component {
state = {
value: '',
};
// ruleid: react-legacy-component
componentWillMount(nextProps) {
this.setState({ value: nextProps.value });
}
handleChange = (e) => {
this.setState({ value: e.target.value });
};
render() {
return <input value={this.state.value} onChange={this.handleChange} />;
}
}
```

```javascript
class Test1 extends React.Component {
state = {
value: '',
};
// ruleid: react-legacy-component
componentDidMount(nextProps) {
this.setState({ value: nextProps.value });
}
handleChange = (e) => {
this.setState({ value: e.target.value });
};
render() {
return <input value={this.state.value} onChange={this.handleChange} />;
}
}
```