在 React 中显示当前年份

在 React 中,你可以像这样渲染当前年份:

<div>{new Date().getFullYear()}</div>

下面的完整示例显示了版权符号以及当前年份(您可以在网上随处看到)。

截屏:

在 React 中显示当前年份

编码:

// KindaCode.com
// src/App.js
function App() {
  return <div style={styles.container}>
    <h1 style={styles.copyright}>&copy;{new Date().getFullYear()} KindaCode.com</h1>
  </div>;
}

const styles = {
  container: {
    width: '100vw',
    height: '100vh',
    display: 'flex',
    alignItems: 'center',
    justifyContent: 'center',
  },
  copyright: {
    color: 'blue'
  }
};

export default App;