Git Product home page Git Product logo

react-live-clock's Introduction

react-live-clock npm

Gitter Dependencies Dev Dependencies

React clock with time-zones DEMO

Installation

NPM

npm install --save react react-live-clock

Don't forget to manually install peer dependencies (react) if you use npm@3.

Demo

http://pvoznyuk.github.io/react-live-clock

Usage

import React  from 'react';
import Clock from 'react-live-clock';

exports default class MyComponent extends React.Component {
    render() {
        <Clock format={'HH:mm:ss'} ticking={true} timezone={'US/Pacific'} />
    }
}

Outputs:

<time>10:15:34</time>

** Shows current time for 'US/Pacific' timezone and updates every second

React Native

React Native requires that you wrap text in a <Text> like this:

import { Text, View } from "react-native";
import Clock from "react-live-clock";

export default function ClockPage() {
  return (
    <View>
      <Text>Clock page</Text>
      <Clock element={Text} />
    </View>
  );
}

If you don't you will get the error Invariant Violation: Text strings must be rendered within a <Text> component.

Formatting

you can use any formatting from moment.js date library

Properties

Property Type Default Value Description
date timestamp or string current date Date to output, If nothing is set then it take current date.
format string 'HH:MM' Formatting from moment.js library.
locale string null Changes the language of the component via prop
filter function (date: String) => date Filtering the value before the output .
timezone string null If timezone is set, the date is show in this timezone. You can find the list. here, the TZ column.
ticking boolean false If you want the clock to be auto-updated every interval seconds.
blinking boolean, string false If you want the clock's last colon to blink. Set it to all to make them all blink.
noSsr boolean false Makes the component not render on the server to fix mismatch.
interval integer 1000 Auto-updating period for the clock. 1 second is a default value.
className string null Extra class.
style CSSProperties null CSSProperties Customized inline style .
children string null date can be set as a children prop.
onChange function ({output, previousOutput, moment}) => {} callback function on each output update

Development and testing

Currently is being developed and tested with the latest stable Node 7 on OSX and Windows.

To run example covering all ReactLiveClock features, use npm start dev, which will compile src/example/Example.js

git clone [email protected]:pvoznyuk/react-live-clock.git
cd react-live-clock
npm install
npm start dev

# then
open http://localhost:8080

Tests

# to run tests
npm start test

# to generate test coverage (./reports/coverage)
npm start test.cov

# to run end-to-end tests
npm start test.e2e

License

This software is released under the MIT license. See LICENSE for more details.

Contributors

react-live-clock's People

Contributors

anthony0030 avatar bjoernkw avatar bughunter727 avatar cihanaksoy avatar damiangreen avatar dependabot[bot] avatar kazoottt avatar manojsethi avatar nestarz avatar pvoznyuk avatar samchou19815 avatar tindalia avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

react-live-clock's Issues

Server time instead of client time

Hello,

Can I show clock that ticking={true} every interval={1000} to start from the server time?
Passing date={new Date} is not solving the issue, nor ticking after set.

Regards

Invariant Violation: Text strings must be rendered within a <Text> component.

This is my render function:

render() {
    return (
      <View style={styles.container}>
        <Text style={styles.text}>Hello World!
          <Clock format={'HH:mm:ss'} ticking={true} timezone={'US/Pacific'} />
        </Text>
        
      </View>
    );
  }

There is an error coming up:

Invariant Violation: Text strings must be rendered within a component.

Moving outside and even doesn't fix the problem.

Can I have ticking when using a custom date?

I'm passing in a date that I'm basing on a gmtDifference value I have access to.

If I don't pass in a date and use ticking={true} I get the desired effect: the time is the current date and the ticking happens once per second.

If I DO pass in a date and use ticking={true}, I get no ticking effect.

[Feature Request] Add blink to the colon to indicate pass of time

Could you add a boolean property blink that controls if the colon blinks every half interval?

If blink==false, then this component works like without this feature.
If blink==true, and assume interval==1000, then the colon would toggle every interval/2 millisecond:

For example current time is 10:24:33

Time 0 10:24:33
Time 500 10:24 33
Time 1000 10:24:34
Time 1500 10:24 34
Time 2000 10:24:35

PS: If the output time format is HH:MM, then blink that colon instead.

Possible Memory Leak?

Hi there! I've been using this as a small clock in my table table toolbar in my SPA app. However, when I navigate away from the page that this lib is mounted on, I receive the following warning:
Screen Shot 2020-07-17 at 1 14 41 PM

I'd be happy to provide any other information or even a small repro, just let me know.

onChange function not working.

Tried the following implementation with version 3.1.0 and I am getting no console log from onChange function.

import React from "react"
import ReactDOM from "react-dom"
import Clock from "react-live-clock"

import "./styles.css"

console.log("test")

function App() {
  return (
    <div className="App">
      <Clock
        format={"HH:mm:ss"}
        ticking={true}
        onChange={() => console.log("tick")}
      />
    </div>
  )
}

const rootElement = document.getElementById("root")
ReactDOM.render(<App />, rootElement)

Nothing in console. You can view and modify in codesandbox here: https://codesandbox.io/s/880zo5v9ml

Unable to Style Clock

<Clock format={"h:mm:ssa"} style={{ fontSize: "24px", color: "green" }} ticking={true} />
I just updated the version and the inline CSS stops working.

<Clock format={"h:mm:ssa"} ticking={true} className="bigclock" />

I also tried this and also didn't work.

Is there any examples how to make it work?

Thanks for your help.

resolve react-moment?

Is there any trick to this?

./node_modules/react-live-clock/lib/Component.js
Module not found: Can't resolve 'react-moment' in '/Users/nolan/clock/node_modules/react-live-clock/lib'

I'm following documentation/ youtube and seem to keep runing into this...

TimeZone clock is not changed according to timezone prop.

Clock is not recalculate to new Time Zone after propery timeZone is change dynamically.

Example:

locale = 'Australia/Sydney'
after oneMinute i am setting new TimeZone:

locale = 'Europe/London'

but the Clock component not changing the time based on the new zone.

<Clock className="world_clock__item" format={format} ticking={true} timezone={locale}/>

Unable to Use

Hello,
i m getting below error though timezone is installed
/node_modules/react-live-clock/lib/Component.js
Module not found: Can't resolve 'moment-timezone' in 'D:\testmode\node_modules\react-live-clock\lib'

TypeError: localizedTime.tz is not a function

ReactLiveClock.render
node_modules/react-live-clock/lib/Component.js:76
73 | var localizedTime = (0, _moment2.default)(dateValue);
74 |
75 | if (timezone) {

76 | localizedTime.tz(timezone);
77 | }
78 |
79 | var formattedTime = localizedTime.format(format);

Doesn't render anything on first pass.

I'm using this component inside of a ReactFitText component and as it doesnt appear to render anything on the first pass, the text isn't fitted (resized). Here's a section of my code with the workaround:

  public componentDidMount() {
    this.forceUpdate(); // Hack because text doesn't appear fitted on first render.
  }

  public render() {
    return (
      <Widget options={this.props.options}>
        <div role="presentation" className={s.root}>
          <ReactFitText compressor={0.3}>
            <h1 className={s.text}>
              <Clock format="HH:mm" ticking interval={1000} />
            </h1>
          </ReactFitText>
        </div>
      </Widget>
    );
  }

Uncaught TypeError: Cannot set properties of undefined (setting 'format')

in case of using this module with VITE

react-dom.development.js:26591 Uncaught TypeError: Cannot set properties of undefined (setting 'format')
at F (:3000/node_modules/.vite/react-live-clock.js?v=b732fa8d:415:32)
at Object.l (:3000/node_modules/.vite/react-live-clock.js?v=b732fa8d:432:46)
at Object. (:3000/node_modules/.vite/react-live-clock.js?v=b732fa8d:433:62)
at n (:3000/node_modules/.vite/react-live-clock.js?v=b732fa8d:28:24)
at Module. (:3000/node_modules/.vite/react-live-clock.js?v=b732fa8d:440:9)
at n (:3000/node_modules/.vite/react-live-clock.js?v=b732fa8d:28:24)
at :3000/node_modules/.vite/react-live-clock.js?v=b732fa8d:55:22
at :3000/node_modules/.vite/react-live-clock.js?v=b732fa8d:56:8
at :3000/node_modules/.vite/react-live-clock.js?v=b732fa8d:20:82
at node_modules/react-moment/dist/index.js (:3000/node_modules/.vite/react-live-clock.js?v=b732fa8d:21:6)
at __require2 (:3000/node_modules/.vite/chunk-VNVKLEIO.js?v=b732fa8d:45:44)
at node_modules/react-live-clock/lib/Component.js (:3000/node_modules/.vite/react-live-clock.js?v=b732fa8d:2034:24)
at __require2 (:3000/node_modules/.vite/chunk-VNVKLEIO.js?v=b732fa8d:45:44)
at node_modules/react-live-clock/lib/index.js (:3000/node_modules/.vite/react-live-clock.js?v=b732fa8d:2146:26)
at __require2 (:3000/node_modules/.vite/chunk-VNVKLEIO.js?v=b732fa8d:45:44)
at :3000/node_modules/.vite/react-live-clock.js?v=b732fa8d:2152:32

Change Timezone

Is it possible to change the timezone? I tried to use a state-variable but the clock does not update on timezone changes. The following however seems to work:

<Clock key={clockTimezone} format={"HH:mm:ss"} ticking={true} timezone={clockTimezone} />

Unfortunately I receive the following error message in console:

Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in a useEffect cleanup function

locale as prop

I'm trying to set another language on the component to set the day and Month on the right language.

Here's what I'm trying:

<Clock format={'ddd DD MMMM'} ticking={true} locale={'es'}/>

I hope someone can help me on seeing my mistake

TypeError: Super expression must either be null or a function

I have error that happening only in production.

Operation systems difference:

dev - windows.
production - linux.

tnx for any help

typeError

Untitled1

Untitled

EDIT:

Solved by downgrade react-live-clock to version 4.0.5.

react-live-clock version 5.0.13 have a dependency on "react-moment": "^1.1.1"

and this one make it not working on produciton.
its a bug.

can't change style format

component page

import React  from 'react';
import Clock from 'react-live-clock';

export default class Mytime extends React.Component {
    render() {
        return(
        <Clock format={'HH:mm A'} ticking={true} timezone={'Asia/Bangkok'}  />
        )
    }
}

main page

var des2 ={
  color: 'white',
  marginLeft: '10px'

}
<Mytime style={des2}  />

Doesn't work with iOS devices.

Tested in both desktop and Android device, worked but when access it using my iPhone, shows invalid date instead.

Any workaround?

Timezone attribute not working

I'm currently using version ^6.0.0 along with "moment-timezone": "^0.5.34" and "react-moment": "^1.1.2"

When setting the timezone property like this
<Clock format="HH:MM" timezone="US/Pacific" ticking />

it still shows my local time and I'm in the UK. I've tried a number of different timezones and none of them are loading with the specified timezomes.

No `filter` prop?

The README speaks of a filter prop but I checked the source code and it doesn't even seem to exist?

TimeZone not apply to date?

I need to output Qua, 5 Mai, 2021, based on my timeZone location (BR)
But it's showing Wed, May 5th, 2021, do this library don't change the location for days/months?
using 'ddd, MMMM Mo, YYYY' and
'America/Belem' timeZone

Warning: Prop `dateTime` did not match.

I added the clock to my next.js app, but in the console I get an error.

the error is like this

Warning: Prop `dateTime` did not match. Server: "1653171979698" Client: "1653171980060"
   at time
   at n (webpack-internal:///./node_modules/react-moment/dist/index.js:11:16590)
   at ReactLiveClock (webpack-internal:///./node_modules/react-live-clock/lib/Component.js:30:24)
   at div
   at TimeZone

My component looks like this:

   <Clock
        format={"h:mm A"}
        timezone={"Europe/Stockholm"}
        className="text-white text-2xl underline"
        ticking={true}

      />

Any ideas what's up?

'blinking' prop only blinks last colon

While the blinking property currently works fine if the format only has a single colon, e.g. 'h:mm a', when the format contains multiple colons, e.g. 'h:mm:ss a', only the last one blinks.
Is there a reason not all colons should be set to blink?

Further, is there a reason this property is not currently documented in the readme?

React 17 Support

npm ERR! While resolving: [email protected]
npm ERR! Found: [email protected]
npm ERR! node_modules/react
npm ERR!   react@"17.0.1" from the root project
npm ERR!
npm ERR! Could not resolve dependency:
npm ERR! peer react@"^16.8" from [email protected]
npm ERR! node_modules/react-live-clock
npm ERR!   react-live-clock@"5.0.12" from the root project

how can i fix useEffect clean up error?

Error message :
Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in a useEffect cleanup function.

how can i fix this problem?

Documentation mistake?

Nice utility, thanks.

In your documentation, I believe you mean Do where you have Mo.

<Clock
date={'1997-12-31T14:15:23+01:00'}
format={'dddd, MMMM Mo, YYYY, h:mm:ss A'} />

Mo is producing the month, whereas Do would produce the day.

Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in a useEffect cleanup function. in ReactLiveClock

Hi,

This is a wonderful plugin.
I have used this. It works well except when I click the back button on the browser, it will throw this:

Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in a useEffect cleanup function. in ReactLiveClock

I am using Class component. May I know how to fix this?

Thank you.

Dependencies and react version

npm WARN [email protected] requires a peer of moment@^2.29.1 but none is installed. You must install peer dependencies yourself.

npm WARN [email protected] requires a peer of moment-timezone@^0.5.33 but none is installed. You must install peer dependencies yourself.

npm WARN [email protected] requires a peer of [email protected] but none is installed. You must install peer dependencies yourself.

npm WARN [email protected] requires a peer of react@^16.13.1 but none is installed. You must install peer dependencies yourself.

npm WARN [email protected] requires a peer of typescript@>=2.8.0 || >= 3.2.0-dev || >= 3.3.0-dev || >= 3.4.0-dev || >= 3.5.0-dev || >= 3.6.0-dev || >= 3.6.0-beta || >= 3.7.0-dev || >= 3.7.0-beta but none is installed. You must install peer dependencies yourself.

Dependencies are not installed directly with the library? Also the react version is old.

React 18

No install posible with react v18

Changing moments local doesn't affect Clock

Hi,

if you change moments locale globally on the fly (moment.locale('de')), Clock is not changing it's format.

So if you use a format like 'ddd DD. MMMM, HH:mm', the format stays on the locale initially loaded.

Thanks
Chris

s.tz is not a function

After updating my package.json to the latest versions, I am not getting the below Error.

s.tz is not a function
./node_modules/react-moment/dist/index.js:11

"react": "^18.1.0",
"react-live-clock": "^5.7.0",
"moment": "^2.29.4",
"moment-timezone": "^0.5.34",
"react-moment": "^1.1.2",

Is anyone else having a similar issue?

Can't resolve 'moment-timezone'

Getting the error

./node_modules/react-live-clock/lib/Component.js
Module not found: Can't resolve 'moment-timezone' in '[...]/node_modules/react-live-clock/lib'

with

"react-live-clock": "^5.0.9",

can't change style format

component page

export default class Mytime extends React.Component {
    render() {
        return(
        <Clock format={'HH:mm A'} ticking={true} timezone={'Asia/Bangkok'}  />
        )
    }
}

main page


var des2 ={
  color: 'white',
  marginLeft: '10px'

}

<Mytime style={des2} />

unmount live clock

i used this (react-live-clock) component inside my home.js component of App.js, but when i change route, react throws and error about react update of unmount component.
to solve it i tried using useEffect hook:

const [timeclock , settimeclock] = useState([]); 
   
    useEffect(() => {
        time();
        
},[]);
const time = async ()=> {
    
    const data =  (<Clock  format={'HH:mm:ss A'} ticking={true} timezone={'America/Toronto'} />)
    
    settimeclock(data);
    
}

but still this throws error
Screen Shot 2020-04-02 at 14 31 19

onReady

Thanks for this great utility!

I was wondering if along with the onChange function there could also be an onReady, that triggers as soon as the clock starts running.

Right now if I set the interval to one minute, the first onChange runs one minute after the component renders. But it'd be great to have an "instant" callback.

Thanks!

Unable to use `className` prop in typescript

The code is like this.

<Clock format="HH:mm:ss" ticking={true} className="classForClock" />

It works with JSX, but it will cause error TS2769 (No overload matches this call) in typescript.

I think className is not defined in index.d.ts. While I add readonly className?: string; like below it might work well.

interface Props {
  readonly children?: string;
  readonly date?: number | string;
  readonly format?: string;
  readonly interval?: number;
  readonly ticking?: boolean;
  readonly timezone?: string;
  readonly filter?: () => void;
  readonly onChange?: () => void;
  readonly className?: string;
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.