jossmac / jossmac/react-view-transition

Use getDerivedStateFromProps instead of UNSAFE

Open
#1 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
7
Forks
2
PR merge metrics
No merged PRs in 30d

Description

You were wondering how to get rid of your UNSAFE methods, the way is to initialize your `index` in the state and then use `this.state.index` in the `childFactory`:

```jsx
export default class TransitionProvider extends Component {
constructor(props) {
super()
this.state = {
height: 0,
index: props.index,
isNext: false,
}
}

static getDerivedStateFromProps(nextProps, state) {
const index = nextProps.index
const isNext = nextProps.index > state.index

return {index, isNext, ...state}
}

childFactory = child => {
const {state} = this
const {index, isNext} = state
const isLeaving = child.props.index !== index
const exitPosition =
(isNext && isLeaving) || (!isNext && !isLeaving) ? '-100%' : '100%'

return cloneElement(child, {exitPosition})
}

// ...
}
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.