Skip to content

Commit

Permalink
feat: add autoFreeze prop
Browse files Browse the repository at this point in the history
  • Loading branch information
CJY0208 authored May 10, 2024
1 parent 2e3a800 commit 9865efb
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 6 deletions.
2 changes: 2 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ export interface CacheRouteProps extends RouteProps {
unmount?: boolean
saveScrollPosition?: boolean
multiple?: boolean | number
autoFreeze?: boolean
}

export declare class CacheRoute extends React.Component<CacheRouteProps> {}
export default CacheRoute

export interface CacheSwitchProps extends SwitchProps {
which?: (element: React.ElementType) => boolean
autoFreeze?: boolean
}

export declare class CacheSwitch extends React.Component<CacheSwitchProps> {}
Expand Down
6 changes: 4 additions & 2 deletions src/components/CacheRoute.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ export default class CacheRoute extends Component {
render: PropTypes.func,
children: PropTypes.oneOfType([PropTypes.func, PropTypes.node]),
computedMatchForCacheRoute: PropTypes.object,
multiple: PropTypes.oneOfType([PropTypes.bool, PropTypes.number])
multiple: PropTypes.oneOfType([PropTypes.bool, PropTypes.number]),
autoFreeze: PropTypes.bool
}

static defaultProps = {
Expand All @@ -39,6 +40,7 @@ export default class CacheRoute extends Component {
saveScrollPosition,
computedMatchForCacheRoute,
multiple,
autoFreeze,
...restProps
} = this.props

Expand Down Expand Up @@ -87,7 +89,7 @@ export default class CacheRoute extends Component {
const renderSingle = props => (
<CacheComponent {...props}>
{cacheLifecycles => (
<Updatable when={isMatch(props.match)}>
<Updatable when={isMatch(props.match)} autoFreeze={autoFreeze}>
{() => {
Object.assign(props, { cacheLifecycles })

Expand Down
4 changes: 2 additions & 2 deletions src/components/CacheSwitch.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ class CacheSwitch extends Switch {
}

render() {
const { children, which } = this.props
const { children, which, autoFreeze } = this.props
const { location, match: contextMatch } = this.getContext()

let __matchedAlready = false

return (
<Updatable when={isMatch(contextMatch)}>
<Updatable when={isMatch(contextMatch)} autoFreeze={autoFreeze}>
{() => (
<SwitchFragment>
{React.Children.map(children, element => {
Expand Down
4 changes: 2 additions & 2 deletions src/core/Updatable/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ class Updatable extends Component {
shouldComponentUpdate = ({ when }) => when
}

export default (props) => (
<DelayFreeze freeze={!props.when}>
export default ({ autoFreeze = true, ...props }) => (
<DelayFreeze freeze={autoFreeze && !props.when}>
<Updatable {...props} />
</DelayFreeze>
)

0 comments on commit 9865efb

Please sign in to comment.