const getTime = () => {
    const today = new Date();
    const time = today + ' ' + today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
    return {time};
}

export default function UpdateButton() {
    const classes = useStyles();
    const today = new Date();
    const time = today + ' ' + today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
    return (
        <div>
            <Button variant="contained" className={classes.button}>
                Update
            </Button>
            <text>Last Updated: {getTime}</text>
        </div>
    )
};

Saya mencoba merender waktu dalam tag teks tetapi terus melempar kesalahan. Bagaimana cara mengatasi masalah ini. Ini berada di bawah kerangka reaksi.

0
user11528729 27 Oktober 2019, 00:24

2 jawaban

Jawaban Terbaik

Fungsi getTime harus mengembalikan string dan bukan objek, dan Anda harus memanggilnya di BEJ - getTime():

const getTime = () => {
  const today = new Date();
  const time = today + ' ' + today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
  return time;
}

function UpdateButton() {
  return (
    <div>
      <button variant="contained">
        Update
      </button>
      <text>Last Updated: {getTime()}</text>
    </div>
  )
};

ReactDOM.render(
  <UpdateButton /> ,
  root
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

<div id="root"></div>
2
Ori Drori 26 Oktober 2019, 21:28

Karena Anda tidak membagikan kesalahan, saya kira itu karena Anda mengembalikan fungsi di BEJ Anda:

const getTime = () => {
    const today = new Date();
    const time = today + ' ' + today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
    return time;
}

export default function UpdateButton() {
    const classes = useStyles();
    const today = new Date();
    const time = today + ' ' + today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
    return (
        <div>
            <Button variant="contained" className={classes.button}>
                Update
            </Button>
            <text>Last Updated: {getTime()}</text>
        </div>
    )
};
1
Melchia 26 Oktober 2019, 21:28