如何通过 ref 获取输入(StyledComponent)值

How to get the input(StyledComponent) value by ref

我正在尝试为样式化组件设置引用。

已使用 const Input = styled.input 创建组件。但我无法通过 ref 获得输入值。

我的代码:

const _Input = styled.input`
  background-image: linear-gradient(#20aee3, #20aee3), linear-gradient(#bfbfbf, #bfbfbf);
  border: 0 none;
  border-radius: 0;
  box-shadow: none;
  float: none;
  background-color: transparent;
  background-position: center bottom, center calc(100% - 1px);
  background-repeat: no-repeat;
  background-size: 0 2px, 100% 1px;
  padding: 0;
  transition: background 0s ease-out 0s;
  color: #bfbfbf;
  min-height: 35px;
  display: initial;
  width: 100%;
  outline: none;
  font-size: 15px;
  &:focus {
      background-size: 100% 2px, 100% 1px;
      outline: 0 none;
      transition-duration: 0.3s;
      color: #525252;
    }
`;

...
<_Input type='text' ref="name" placeholder="name .."/>

您需要使用

innerRef="name"

对于带样式的组件 ref 将不起作用,您必须使用 innerRef。

好的,我想我明白了。 您无法从样式化的组件中获取 ref。您需要将一些参数传递给它,然后将其作为引用传递给您正在使用的组件。 例如,您可以查看此 link: https://github.com/styled-components/styled-components/issues/1151