diff options
author | lonkaars <l.leblansch@gmail.com> | 2021-03-11 14:40:47 +0100 |
---|---|---|
committer | lonkaars <l.leblansch@gmail.com> | 2021-03-11 14:40:47 +0100 |
commit | be9e5197d87e3ebefa43d96a49bbd6843edaf3e1 (patch) | |
tree | d119cf9c85323e126a8504f4562a7a5af2dfa3ef | |
parent | 709bd2fddcea033d162f61a076ec82ce34969887 (diff) |
switch default state reads correctly
-rw-r--r-- | components/ui.tsx | 12 |
1 files changed, 10 insertions, 2 deletions
diff --git a/components/ui.tsx b/components/ui.tsx index f17ae5d..8fa9657 100644 --- a/components/ui.tsx +++ b/components/ui.tsx @@ -1,4 +1,4 @@ -import { Component, CSSProperties, ReactNode, useState } from "react"; +import { Component, CSSProperties, ReactNode, useState, useEffect } from "react"; import SearchIcon from '@material-ui/icons/Search'; import CheckBoxOutlineBlankIcon from '@material-ui/icons/CheckBoxOutlineBlank'; @@ -166,7 +166,15 @@ export function CheckBox(props: { id?: string; onclick?: (state: boolean) => void; }) { - var [on, setOn] = useState(props.state || false); + var [gotDefaultState, setGotDefaultState] = useState(false); + var [on, setOn] = useState(props.state); + + useEffect(() => { + if (gotDefaultState) return; + setOn(props.state); + if (typeof props.state !== "undefined") setGotDefaultState(true); + }); + var toggle = () => { setOn(!on); props.onclick && props.onclick(!on); |