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

时间:2018-05-01 07:42:27

标签: javascript reactjs input 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 .."/>

2 个答案:

答案 0 :(得分:1)

您需要使用

  

innerRef = “名称”

对于样式化组件引用将无法工作,您必须使用innerRef。有关详细信息,请参见下面的结帐链接。

https://github.com/styled-components/styled-components/blob/master/docs/tips-and-tricks.md

答案 1 :(得分:0)

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

相关问题