简体   繁体   中英

how to declare hooks inside object?

I'm using hooks like this

const Ref1 = useRef();
const Ref2 = useRef();
const AllRefs = { Ref1, Ref2 };

is there anyway to write this in shorthand maybe something like this:

const AllRefs = { Ref1:useRef(), Ref2:useRef()};

should it be like this?

import React, { useRef } from "react";

export default function App() {
  const arr = [1, 2, 3];

  const refs = useRef([]);

  return (
    <div className="App">
      {arr.map((item, index) => {
        return (
          <div
            key={index}
            ref={(element) => {
              refs.current[index] = element;
            }}
          >
            {item}
          </div>
        );
      })}
    </div>
  );
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM