score:1

Accepted answer

useref returns a mutable ref object whose .current property is initialized to the passed argument (initialvalue). the returned object will persist for the full lifetime of the component.

const ispressbackspaceref = react.useref(false);
const keydownpositionref = react.useref({});
const onkeydown = (e) => {
    // this is wrong syntax
    // this.keydownposition or let keydownposition
    keydownpositionref.current = {
      start: e.target.selectionstart,
      end: e.target.selectionend
    };
    switch (e.key) {
      case "backspace":
        ispressbackspaceref.current = true; // this is wrong syntax ????
        break;
      default:
        break;
    }
  };
const onchange = (e) => {
    const { end } = keydownpositionref;
    if (ispressbackspaceref.current) {
      const length = end - e.target.selectionend;
      alert(`you delete ${length} character`);
    }
    ispressbackspaceref.current = false;
};

score:0

in my experience you don't use the this keyword when working with function components. instead you use hooks like usestate. check the following video for getting started with hooks: https://www.youtube.com/watch?v=o6p86uwfdr0&ab_channel=webdevsimplified


Related Query

More Query from same tag