简体   繁体   English

我想在单击时更改 div 的显示 -React Styled Component

[英]I wan to change display of a div when clicked -React Styled Component

I am creating a div using styled component.我正在使用样式化组件创建一个 div。 I want to change the visibility of the div on button clicked,我想在单击按钮时更改 div 的可见性,

const Category = () => {
  const [showCategory, setShowCategory] = useState(false)


  useEffect(() => {
    setShowCategory(false)
  }, [])
  return (
<button onClick={() => {  setShowCategory(true)}}>
   New Category
</button>
        <AdminInputStyle>
          <form>
            <form-group>
              <label>Add Category</label>
              <input type='text' />
            </form-group>
            <button>Submit</button>
          </form>
        </AdminInputStyle>

  )

} }

Here's the styled component这是样式化的组件

const AdminInputStyle = styled.div`
  display: ${(d) => (d.showCategory ? 'show' : 'hidden')};
`

I have an example, but in the case we will use a Button.我有一个例子,但在这种情况下我们将使用一个按钮。 Clicking it will alter the visibility.单击它会改变可见性。

  1. You must pass a property to the styled component if you want it to be visible based on that prop.如果您希望它基于该属性可见,则必须将属性传递给样式化组件。 In your example, you don't pass a prop to the styled component in this scenario, which is why the component cannot detect if it should be visible or not.在您的示例中,在这种情况下您没有将道具传递给样式化的组件,这就是组件无法检测到它是否应该可见的原因。

  2. You will need to / can use the css function from the styled-components library.您将需要/可以使用styled-components库中的css 8CBA22E28EB17B5F5C6AE2A266AZ function。 This can help you return styles based on the properties your styled-component will have.这可以帮助您根据样式组件将具有的属性返回 styles。 In this example, our property that we pass to the button will be called visible .在这个例子中,我们传递给按钮的属性将被称为visible

 import React from 'react'; import PropTypes from 'prop-types'; import styled, { css } from 'styled-components/macro'; const StyledButton = styled.button` border-radius: 3px; color: white; background-color: green; cursor: pointer; width: 100px; height: 50px; ${({ visible }) => { return css` visibility: ${visible? 'visible': 'hidden'}; `; }} `; export default function Button({ children, visible, onClick }) { return ( <StyledButton visible={visible} onClick={onClick}> {children} </StyledButton> ); } Button.propTypes = { children: PropTypes.node, visible: PropTypes.bool, onClick: PropTypes.func, };

You can see that passing the visible prop will enable the button to alter its' styles based on whether that property is true or false.您可以看到传递visible属性将使按钮能够根据该属性的真假来更改其 styles。 We utilize a function within the component that returns the css function and this will control the visibility css property.我们在返回 css function 的组件中使用 function ,这将控制visibility ZC7A628CBA2AE2A28EB16AZ.75CBA2AE2AF14Z 属性

Here is how we utilize the button and pass props to it from another component;下面是我们如何利用按钮并将道具从另一个组件传递给它; in this example just the App.js file:在这个例子中只是 App.js 文件:

 import React, { useState } from 'react'; import './App.css'; import Button from './components/Button'; function App() { const [visible, setVisible] = useState(true); function handleClick() { setVisible(;visible); } return ( <div className="App"> <Button visible={visible} onClick={handleClick}> Click </Button> </div> ); } export default App;

You can try something like this too, show when you need to show the add category when you press add category您也可以尝试这样的事情,当您按下添加类别时显示何时需要显示添加类别

return (
    <>
      <button
        onClick={() => {
          setShowCategory(true);
        }}
      >
        New Category
      </button>
      {showCategory && (
        <AdminInputStyle>
          <form>
            <form-group>
              <label>Add Category</label>
              <input type="text" />
            </form-group>
            <button>Submit</button>
          </form>
        </AdminInputStyle>
      )}
    </>
  );

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

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