Comments (6)
@nmn There should be an option to use a static time that we can pass in as a prop instead of Date.now() for the initial render, but it can use Date.now() on subsequent updates. The reason for this is that React-timeago is being used for server side rendering but because of the way that it uses Date.now() I will always end up with Invalid checksum with server-side rendering
I believe React-timeago should support this use case for better integration with React
from react-timeago.
@joshrowley This is an interesting idea that basically no one has ever asked for.
My basic philosophy with react-timeago is that I'm happy to add new features as long as people who don't want that feature don't have to pay for it. This is why language support was added with custom formatter functions that are imported separately.
If you have an idea like that I'm all for it. But before that happens, here's a few things to consider:
- You can technically customize the output completely using a formatter function. This function gets a raw difference in milliseconds so you could easily add your own offset to it, and then use the built in formatters to convert it to a string. However, formatter functions need to be synchronous which means a server-call is not going to be straightforward.
- It's probably easy enough to make a Date.now() a prop on the component, with a default value. But again, this will probably need to be synchronous, so a server-call won't exactly be straightforward.
Any other ideas?
from react-timeago.
Hey @nmn , I was able to use the formatter
function to do this (I didn't need any async behavior because I send the server time in the response headers with my data request which happens before any components needing TimeAgo render). Worked out great 👍 , thanks! This can be closed unless you wanna keep it open for @allenylzhou's comment.
from react-timeago.
@allenylzhou I like this solution the best so far:
Ask for an initial render string which is static. Use componentDidMount
(which only fires on the client side) to then revert to the current behaviour.
This way, the server-side render and the first render of the client-side will match up. (It'll just be a static string). But after that it will become a dynamic component again.
That said, this behaviour can be put in a container component. I'll add that container component as an optional file you can use from the package, but in the mean time you can do it your self.
class staticContainer extends React.Component {
state = {mounted: false}
componentDidMount () { this.setState({mounted: true}) }
render () {
const {children, ...props} = this.props
if (!this.state.mounted) {
return <span>{children}</span>
}
return <TimeAgo {...props} />
}
}
@joshrowley This might be a better solution for you too.
from react-timeago.
Another use case for this is for Jest testing - if you snapshot a component with a TimeAgo, subsequent tests will have a different Date.now and fail. One solution is to mock Date.now itself, but it may have unintended side effects.
from react-timeago.
This feature was added and released. You can now override a the 'now' prop.
from react-timeago.
Related Issues (20)
- in persian formatter digits do not convert to persian digits HOT 3
- Add React 17 support HOT 5
- Unclear breaking changes HOT 2
- Add changelog note for version 5 HOT 4
- Live stops if Date value is changed. HOT 8
- Memory leak HOT 1
- just now with formatter + buildFormatter HOT 3
- Prefix prop? HOT 1
- support for fuzzy intervals ("a moment ago", etc) HOT 3
- React version
- Can we have more verbose release notes? HOT 1
- [FeatureRequest] Replace "XXX from now" to "in XXX"
- Updating date causes display problems HOT 1
- Add "less then a minute ago" option
- why show this type error on console? HOT 2
- Immediately after using react-timeago it show Error: (0 , React.useState) is not a function HOT 3
- Ago legend shows up before time
- Disable tooltip HOT 1
- Add 'Never' option
- Styling HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-timeago.