Sparrowan
Sparrowan

Reputation: 1

Convert React function component to a React Class Component

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

Upvotes: 0

Views: 55

Answers (1)

Caio Doneda
Caio Doneda

Reputation: 51

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} />;
  }
}

Upvotes: 1

Related Questions