Search code examples
javascriptreactjsinputref

How to get the input(StyledComponent) value by ref


I am trying to set the ref for a styled components.

The component has been created with const Input = styled.input. but i can't get value of input by ref.

My code :

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 .."/>

Solution

  • You need to use

    innerRef="name"

    For styled component ref will not work instead you have to use innerRef.