Skip to content

Commit

Permalink
[mui#1126] Fix rerendering loop
Browse files Browse the repository at this point in the history
  • Loading branch information
dmtrKovalenko committed Jun 24, 2019
1 parent 2308bfc commit 20c03ad
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions lib/src/_shared/hooks/usePickerState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,25 @@ import { MaterialUiPickersDate } from '../..';
import { useOpenState } from './useOpenState';
import { BasePickerProps } from '../../typings/BasePicker';
import { getDisplayDate, validate } from '../../_helpers/text-field-helper';
import { useCallback, useDebugValue, useEffect, useMemo, useState } from 'react';
import { useCallback, useDebugValue, useEffect, useMemo, useState, useRef } from 'react';

export interface StateHookOptions {
getDefaultFormat: () => string;
}

const valueToDate = (
const useValueToDate = (
utils: IUtils<MaterialUiPickersDate>,
{ value, initialFocusedDate }: BasePickerProps
) => {
const initialDate = value || initialFocusedDate || utils.date();
const date = utils.date(initialDate);
const nowRef = useRef(utils.date());
const date = utils.date(value || initialFocusedDate || nowRef.current);

return date && utils.isValid(date) ? date : utils.date();
return date && utils.isValid(date) ? date : nowRef.current;
};

function useDateValues(props: BasePickerProps, options: StateHookOptions) {
const utils = useUtils();
const date = valueToDate(utils, props);
const date = useValueToDate(utils, props);
const format = props.format || options.getDefaultFormat();

return { date, format };
Expand Down

0 comments on commit 20c03ad

Please sign in to comment.