带反应钩的样式化组件

时间:2019-06-11 09:22:04

标签: javascript reactjs react-hooks

我正在使用带有样式组件的钩子。单击时,我要显示DropdownContentUI。如何将prop从状态传递到UI组件?

const DropdownContentUI = styled.div`
  display: ${props=>props.isOpen ? 'inline-flex' : 'none'};
  position: absolute;
  background-color: #f9f9f9;
  top: 100%;
  left: 0;
  z-index: 1;
  flex-flow: column;
`;

const DropdownArrows = () => {
  const [isOpen, setTrigger] = useState(false);
  return (
<DropdownArrowsUI onClick={()=> setTrigger(!isOpen)} isOpen>
    <DropdownArrowsWrapperUI>
      <DropdownIconUI width="8" height="11" viewBox="0 0 8 11">
        <path d="M1 4.04L4.04 1L7.08 4.04" stroke="#BACCD8" />
        <path d="M7.08008 6.46L4.04008 9.5L1.00008 6.46" stroke="#BACCD8" />
      </DropdownIconUI>
    </DropdownArrowsWrapperUI>
    <DropdownContentUI>
      <DropdownArrowsWrapperUI>
        <DropdownIconUI width="8" height="7" viewBox="0 0 8 7">
          <path d="M1 4.04L4.04 1L7.08 4.04" stroke="#177FF2" />
          <path d="M4 4V7" stroke="#177FF2" />
        </DropdownIconUI>
      </DropdownArrowsWrapperUI>
    </DropdownContentUI>
  </DropdownArrowsUI>
  );
};

2 个答案:

答案 0 :(得分:3)

.lt-content {
    display: flex;
    flex-flow: column;
}
#board {
    display: grid;
    grid-auto-flow: column;
    height: 100%;
}

如果您仅输入<DropdownContentUI isOpen={isOpen}/> ,它将始终为真

答案 1 :(得分:1)

或者,您可以在这种情况下使用条件渲染。

{isOpen && <DropdownContentUI/>}