Skip to content

Commit

Permalink
Remove flickering when changing page
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenramthun committed Jul 30, 2019
1 parent beba0c9 commit d935779
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/components/widgets/Uenigboks.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useContext, useEffect, useState } from 'react';
import React, { useContext, useEffect, useMemo, useState } from 'react';
import PropTypes from 'prop-types';
import { Checkbox, Input } from 'nav-frontend-skjema';
import { InnrapporteringContext } from '../../context/InnrapporteringContext';
Expand All @@ -14,19 +14,24 @@ Input.propTypes = {
const Uenigboks = ({ label }) => {
const id = label + decodeURIComponent(window.location.pathname);
const innrapportering = useContext(InnrapporteringContext);
const [checked, setChecked] = useState(false);
const [inputValue, setInputValue] = useState('');

const uenighet = useMemo(
() => innrapportering.uenigheter.find(uenighet => uenighet.id === id),
[innrapportering.uenigheter]
);

const [checked, setChecked] = useState(!!uenighet);
const [inputValue, setInputValue] = useState(
uenighet ? uenighet.value : ''
);
const ref = useFocusRef(checked && inputValue === '');

useEffect(() => {
const uenighet = innrapportering.uenigheter.find(
uenighet => uenighet.id === id
);
if (uenighet) {
setChecked(true);
setInputValue(uenighet.value || '');
}
}, [innrapportering.uenigheter]);
}, [uenighet]);

const onCheckboxChange = e => {
setChecked(e.target.checked);
Expand Down

0 comments on commit d935779

Please sign in to comment.