首页 > 解决方案 > ReactJs:Date().toLocaleTimeString 不起作用

问题描述

目前我开始学习 ReactJs。我想在反应中获取当前时间。但是我使用的方法显示了一些错误。我想获取当前时间。

我尝试了一些类似的问题,但对我没有帮助。

我试过的

import React, { useState } from 'react';

let datte = new Date().toLocaleTimeString;

const Click = () => {




    return(
        <>
        <div className="container mt-5 text-center bg-danger rounded p-5 text-white">
            <h2>The current Time is : {datte} </h2>
            <button  className="btn btn-info btn-lg">Get time</button>
        </div>
        </>
    );
   
};

export default Click;

我得到的错误

index.js:1 Warning: Functions are not valid as a React child. This may happen if you return a Component instead of <Component /> from render. Or maybe you meant to call this function rather than return it.
    in h2 (at Click.jsx:13)
    in div (at Click.jsx:12)
    in Click (at src/index.js:7)

那么我该如何解决这些错误..

谢谢

标签: reactjs

解决方案


使用这个 toLocaleTimeString();


推荐阅读