Skip to content

Using in a render() method? #1

Description

@mikelambert

So I understand I can construct a translator object and use _ in my render() method without any problems. But I'm confused how I would go about using date/time formatting functions within a render() method, due to their asynchronous nature. I have a proposed solution below, but I believe this results in a flash of "empty data" before showing the real data (similar to the flash-of-unstyled-text in the HTML world), and am curious if there's a better way...a pure JS solution, a la one of the other libraries out there. Thanks!

class TranslatedDate extends React.Component {
  constructor(props) {
    super(props);
    this.state = {translatedDate: null};
    this.translate();
  }

  async translate() {
    const translatedDate = await new Intl.DateTimeFormat('en-US').format(date);
    this.setState({translatedDate});
  }

  render() {
    return <Text>{this.state.translatedDate}</Text>;
  }
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions