Skip to content
the poor coder

React clock via props

Poor Man
Published
Reading time
2 min read
On this page

React clock via props#

If you want to create a clock component in React and pass the time through props, there are a few steps to take.

Step 1: Create a Clock Component#

Create a new file called Clock.js and import React and PropTypes:

JavaScript

import React from 'react';
import PropTypes from 'prop-types';

class Clock extends React.Component {
  render() {
    return (
      <div>
        {this.props.time}
      </div>
    );
  }
}

Clock.propTypes = {
  time: PropTypes.string.isRequired,
};

export default Clock;

In this example, we have a simple Clock component that takes a time prop and displays it in a div. We also define a prop type for the time prop to make sure it is a string.

Step 2: Pass the Time through Props#

Now we can use our Clock component and pass the time through props. Here is an example:

JavaScript

import React from 'react';
import Clock from './Clock';

class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      time: new Date().toLocaleTimeString(),
    };
  }

  componentDidMount() {
    setInterval(() => {
      this.setState({
        time: new Date().toLocaleTimeString(),
      });
    }, 1000);
  }

  render() {
    return (
      <div>
        <Clock time={this.state.time} />
      </div>
    );
  }
}

export default App;

In this example, we use the setInterval function to update the time state every second. We then pass the time state through props to our Clock component.

Alternative: Using Functional Component#

You can also create a clock component using a functional component instead of a class component:

JavaScript

import React from 'react';
import PropTypes from 'prop-types';

function Clock(props) {
  return (
    <div>
      {props.time}
    </div>
  );
}

Clock.propTypes = {
  time: PropTypes.string.isRequired,
};

export default Clock;

With this method, you don't need to extend the React.Component class. You simply define a function that takes props as an argument and returns JSX.

Conclusion#

In this tutorial, we learned how to create a clock component in React and pass the time through props. We also explored an alternative way of creating the clock component using a functional component. Now you can easily add a clock to your React app!

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC