0

I have a code with refs set using the useState but I would love to convert the code into a class component

  const [imageSliderRef, setImageSliderRef] = useState(null);
  const [textSliderRef, setTextSliderRef] = useState(null);

for the above and render it in the jsx

The code with the hooks and jsx

1 Answer 1

1

If you want to use classes, you can create Refs using React.createRef() and attach the ref to React elements via the ref attribute. Refs are commonly assigned to an instance property when a component is constructed so they can be referenced throughout the component.

class MyComponent extends React.Component {
  constructor(props) {
    super(props);
    this.myRef = React.createRef();
  }
  render() {
    return <div ref={this.myRef} />;
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your answer. I understand refs but I am using a template and they have defined it using the useState as shown above in the image and set it to null. How do I convert the above to a class Component?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.