最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to create dynamic variables and use them with useState() - Stack Overflow

programmeradmin0浏览0评论

I have a condition where I have to create a mon ponent which will be used at different places like given below.

Step 1

Step 2

In both the above steps I have use the same ponent, just the questions asked are different.

   stepOne: [
      {
        title: "Is your age less than 18 ?",
        name: "age",
        answer: 0
      }
    ],
   stepTwo: [
      {
        title: "Have you been to some trip previously ?",
        name: "trip",
        answer: 0
      },
      {
        title: "Do you like adventurous sports ?",
        name: "sportActive",
        answer: 0
      }
    ],

I have successfully managed to do this but with class ponents but now I want to use react hooks "useState()"

Below is the a dumy code to my query. I will be sending step_1 and step_2 in "data" paramameter.

export function YesNo({ data, setAnswer }) {
  for(let i in data){
    const [---] = useState(data[i].answer);
  }

So the question is how to create the dynamic variable and use them with useState(), Is this approach wrong or I didn't got the hooks concept correctly.

I have a condition where I have to create a mon ponent which will be used at different places like given below.

Step 1

Step 2

In both the above steps I have use the same ponent, just the questions asked are different.

   stepOne: [
      {
        title: "Is your age less than 18 ?",
        name: "age",
        answer: 0
      }
    ],
   stepTwo: [
      {
        title: "Have you been to some trip previously ?",
        name: "trip",
        answer: 0
      },
      {
        title: "Do you like adventurous sports ?",
        name: "sportActive",
        answer: 0
      }
    ],

I have successfully managed to do this but with class ponents but now I want to use react hooks "useState()"

Below is the a dumy code to my query. I will be sending step_1 and step_2 in "data" paramameter.

export function YesNo({ data, setAnswer }) {
  for(let i in data){
    const [---] = useState(data[i].answer);
  }

So the question is how to create the dynamic variable and use them with useState(), Is this approach wrong or I didn't got the hooks concept correctly.

Share Improve this question edited Dec 15, 2019 at 20:34 skyboyer 23.8k7 gold badges62 silver badges71 bronze badges asked Dec 15, 2019 at 20:17 AdarshAdarsh 5242 gold badges10 silver badges19 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

You could do something like this:

Basically the magic line is this one [evt.target.name]: {

It is a new feature from ES6 Computed property names

More about it https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer#Computed_property_names

import React, { useState } from "react";
import ReactDOM from "react-dom";
import "./styles.css";

function App() {
  const [steps, setSteps] = useState({});

  function onClick(evt) {
    setSteps({
      [evt.target.name]: {
        value: evt.target.name
      }
    });
  }

  console.log(steps);

  return (
    <div className="App">
      <button onClick={onClick} name="step-one">
        ste one
    </button>
      <button onClick={onClick} name="step-two">
        ste two
    </button>
    </div>
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
发布评论

评论列表(0)

  1. 暂无评论