박형렬
박형렬

Reputation: 397

How to create a lot of variables in react by writing little code

I want to make a lot of variables like the below code. I think if I use the maps function it will work, but it does not work. Could you teach me how to reduce the code?

const name = useInput("");
  const area = useInput("");
  const job = useInput("");
  const company = useInput("");
  const school = useInput("");
  const tall = useInput("");
  const body = useInput("");
  const religion = useInput("");
  const smoking = useInput("");
  const blood = useInput("");
  const birthday = useInput("");
  const kakao = useInput("");
  const image1 = useInput("");
  const image2 = useInput("");
  const image3 = useInput("");
  const introduction = useInput("");

Upvotes: 0

Views: 93

Answers (2)

Jay
Jay

Reputation: 3107

I hope this works for you

Array(16) please change number according to your variables

const [name,area,job,company,school,tall,body,religion,smoking,blood,birthday,kakao,image1,image2,image3,introduction] = Array(16).fill().map(() => useInput(""))

Upvotes: 0

Oleksandr Paiziak
Oleksandr Paiziak

Reputation: 382

Maybe you can convert it to array and object

const arr = ['name', 'area', etc...];
const obj = {};
arr.forEach(key => {
 obj[key] = useInput(key);
}

Upvotes: 1

Related Questions