简体   繁体   中英

how to set a useState constant variable using api variables and dynamic js variables in React.js

I don't know how to search for this so thats why the title is a bit weird,I will just post an example. Basicly I want to know how replace the DATEHERE with the variable date I have created.

import React, { useState } from "react";
import axios from "axios";

function App(loading, result) {
  const [test, setTest] = useState([]);

    var today = new Date(),
        date = "05-01-2021";
    
    const url = "random-api-url"
    
    const getData = async () => {
        const result = await axios.get(url);
        setTest(result.data.example.DATEHERE);
        console.log(result);
      };



 return (
        <div className="App">
          <h1 onClick={getData}>test</h1>
        </div>
      );
    }
    
    export default App;

If your response result.data.example coming as an object?

{"05-01-2021": [] , ...}

if so use result.data.example[date]

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