简体   繁体   English

如何使用状态在 React.js 中显示日期?

[英]How to display date in React.js using state?

I'm trying to display date using React.js but for some reason it's not displaying anything.我正在尝试使用 React.js 显示日期,但由于某种原因它没有显示任何内容。

I'm a beginner so I'm sorry if I'm asking a stupid question, but I can't figure out why it's not working.我是初学者,所以如果我问了一个愚蠢的问题,我很抱歉,但我不知道为什么它不起作用。 Can someone please help me?有人可以帮帮我吗? Thanks so much!非常感谢!

class App extends React.Component {
  state = {
    date: ""
  };

  getDate() {
    var date = { currentTime: new Date().toLocaleString() };

    this.setState({
      date: date
    });
  }

  render() {
    return (
      <div class="date">
        <p> ddd {this.state.date}</p>
      </div>
    );
  }
}

export default App;

You're trying to get the state of the date without explicitly setting it first.您试图在没有先明确设置的情况下获取日期的状态。 With that in mind call the getDate() method in something like, ComponentDidMount : 考虑到这一点,在 ComponentDidMount 中调用 getDate()方法:

class App extends App.Component {
  ...
  componentDidMount() {
    this.getDate();
  }
  ...
}

From there you should be able to retreive it in your render call:从那里你应该能够在你的渲染调用中检索它:

render() {
  return (
    <div class="date">
      <p> ddd {this.state.date}</p>
    </div>
  );
}

Update:更新:

constructor() is probably more suitable for this situation as no external requests are called to retrieve said data: constructor()可能更适合这种情况,因为没有调用外部请求来检索所述数据:

constructor(props) {
  super(props);
  this.state = {
    date: new Date().toLocaleString()
  };
}

Try doing:尝试做:

var date = new Date();
this.setState({ date });

instead of代替

var date = {currentTime: (new Date()).toLocaleString()}
this.setState({date:date });

And check if the current date shows up.并检查当前日期是否出现。

Then check out how to alter the date field with some format.然后查看如何使用某种格式更改日期字段。

Hope this helps you out希望这可以帮助你

Here's a simple way:这是一个简单的方法:

class App extends React.Component {
  state = {date: new Date()}

  render() {
    return (
      <div class="date">
        <p> ddd {this.state.date.toLocaleDateString()}</p>
      </div>
    );
  }
}

export default App;

Cheers!干杯!

I would suggest looking up component lifecycle methods .我建议查找组件生命周期方法

The state hasn't been set because you never call getDate(), so you're just getting an empty string.状态尚未设置,因为您从未调用过 getDate(),因此您只是得到一个空字符串。

import React from "react";

export default class App extends React.Component {
  state = {
    date: ""
  };

  componentDidMount() {
    this.getDate();
  }

  getDate = () => {
    var date = new Date().toDateString();
    this.setState({ date });
  };

  render() {
    const { date } = this.state;

    return <div>{date}</div>;
  }
}

You can use JavaScript codes by using ${} like following:您可以通过使用${}来使用 JavaScript 代码,如下所示:

<span className="xyz">
      {`${new Date().toLocaleString()}`}
</span>

Please note that you need to use the Grave Accent character (ie ` ) around the ${} .请注意,您需要在${}周围使用Grave Accent 字符(即` )。

Or if you want to convert a date-time string to local value, you can follow this solution:或者,如果要将日期时间字符串转换为本地值,可以按照以下解决方案进行操作:

<span className="xyz">
      {`${formData.updatedAt ? new Date(formData.updatedAt).toLocaleString() : ""}`}
</span>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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