1

I have been trying to change input field from one to the other with no avail, when I use .focus it gives me an error of it is not a function.

I will paste my code

import React, { useEffect } from 'react';
import { signup } from '../../assets/images';
import FormDiv from '../shared/Sign-in-div';
import ImageDiv from '../shared/Image-div';
import { Nunito32, Nunito20 } from '../shared/nunito/nunito';
import ImageContainer from '../shared/image-container';
import OtpField from '../shared/otp-field';
import PinkButton from '../shared/button-color-pink';

const SignUpVerification = () => {
  const fieldOne = React.createRef();
  const fieldTwo = React.createRef();
  const fieldThree = React.createRef();
  const fieldFour = React.createRef();

  return (
    <div style={{ display: 'flex' }}>
      <FormDiv style={{ textAlign: 'center' }}>
        <Nunito32
          style={{
            display: 'inline-block',
            textAlign: 'center',
            marginRight: 236,
            marginLeft: 200,
          }}
        >
          Verify your mobile number by entering the code we sent you
        </Nunito32>
        <div style={{ flexDirection: 'row' }}>
          <OtpField
            ref={fieldOne}
            style={{ marginRight: 10.5 }}
            onChange={() => fieldTwo.focus()}
          />
          <OtpField
            ref={fieldTwo}
            style={{ marginRight: 10.5 }}
            onChange={() => fieldThree.focus()}
          />
          <OtpField
            ref={fieldThree}
            style={{ marginRight: 10.5 }}
            onChange={() => fieldFour.focus()}
          />
          <OtpField
            ref={fieldFour}
            style={{ marginRight: 10.5 }}
            onChange={() => fieldFour.focus()}
          />
        </div>

        <PinkButton style={{ marginTop: 75 }}>Continue</PinkButton>
        <Nunito20>Send again</Nunito20>
      </FormDiv>
      <ImageContainer>
        <ImageDiv bg={signup} src={signup} alt="logo" />
      </ImageContainer>
    </div>
  );
};

export default SignUpVerification;

Can someone help pls. And if you know a better way to do it, Ill be grateful for leading me in the right path

Emile Bergeron
  • 17,074
  • 5
  • 83
  • 129
SImon Haddad
  • 742
  • 1
  • 10
  • 24

2 Answers2

2

According to docs

When a ref is passed to an element in render, a reference to the node becomes accessible at the current attribute of the ref.

So try using field.current.focus()

Sumanth Madishetty
  • 3,425
  • 2
  • 12
  • 24
2

Since you have a functional component you should be using useRef. createRef will create a new reference on each render.

https://reactjs.org/docs/hooks-reference.html#useref

Also the reference will expose a property current which you have to use to set focus.

const fieldOne = useRef(null);
...
          <OtpField
            ref={fieldOne}
            style={{ marginRight: 10.5 }}
            onChange={() => fieldTwo.current.focus()}
          />
          <OtpField
            ref={fieldTwo}
            style={{ marginRight: 10.5 }}
            onChange={() => fieldThree.current.focus()}
          />
tony
  • 1,274
  • 1
  • 11
  • 27