简体   繁体   English

通过在页面加载后切换状态来更改样式组件样式

[英]Changing a styled components style by toggling state after page load

I'm able to change a styled components styles based on state but only at page load, if I toggle the state after page load, the state changes successfully but the styles remain the same.我能够根据状态更改样式组件样式,但仅在页面加载时,如果我在页面加载后切换状态,状态更改成功但样式保持不变。 The styled component whose styles I'm trying to change is "S.Search" which is inside of a "Search" component whose parent is "Navbar".我试图更改其样式的样式组件是“S.Search”,它位于父项为“Navbar”的“Search”组件内。 I'm passing "Search" an "isVisible" state as a prop that is toggled by the onClick event handler in the parent component "Navbar".我将“搜索”和“isVisible”状态作为道具传递,该道具由父组件“Navbar”中的 onClick 事件处理程序切换。

The parent component:父组件:


    import React, { useContext, useState } from "react"
    import Search from "../components/Search"
    import * as S from "../styled/Navbar"

    const Navbar = () => {

      const [isVisible, setVisibility] = useState(false)
      return (
        <S.Navbar>
          /* S.SearchButton toggles the isVisible state onclick */
              
              <S.SearchButton onClick={() => setVisibility(!isVisible)}>
                <S.SearchButtonText>Search</S.SearchButtonText>
              </S.SearchButton>
            
            /* Passing isVisible state into the Search component as props */
            
            <Search isVisible={isVisible} setVisibility={setVisibility} />
        </S.Navbar>
      )
    }

    export default Navbar

The search component:搜索组件:


    import React, { useState } from "react"
    import Icon from "../components/Icon"
    import * as S from "../styled/Search"

    const Search = props => {
      return (
        <S.Search>
          <S.InputWrapper>
            <S.SearchButtonIcon>
              <Icon icon={"search"} />
            </S.SearchButtonIcon>
            <S.Input
              type="text"
              placeholder="Search for a character"
              autocomplete="off"
            />
            <S.ChampList></S.ChampList>
          </S.InputWrapper>
        </S.Search>
      )
    }

    export default Search


I'm sure my state is being toggled correctly, but I have no idea why the style isn't being updated when toggling the state after page load.我确定我的状态被正确切换,但我不知道为什么在页面加载后切换状态时样式没有更新。 This is the styled component that has access to the isVisible state via props passed to the search component:这是通过传递给搜索组件的 props 访问 isVisible 状态的样式组件:


    import styled from "styled-components"

    export const Search = styled.div`
      height: ${p => (!p.isVisible ? `0` : `2.5rem`)};
      visibility: ${p => (!p.isVisible ? `hidden` : `visible`)};
      opacity: ${p => (!p.isVisible ? `0` : `100`)};
      display: flex;
      justify-content: center;
      transition: ${p => p.theme.easings.easeOut};

    `

How can I change the style after page load when I toggle the state?切换状态时如何在页面加载后更改样式? Thank you for your time!感谢您的时间!

Your style isnt being applied because you are not passing isVisible prop to Search style-component.您的样式没有被应用,因为您没有将isVisible道具传递给Search样式组件。

You just need to do this in your Search Component :您只需要在您的Search Component执行此操作:

const Search = props => {
      return (
        // Here´s the trick
        <S.Search isVisible={props.isVisible}>
         .....
         .....
         .....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM