Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix duplicate day rendering #1255

Merged

Conversation

Projects
None yet
2 participants
@pedrogarciyalopez
Copy link
Contributor

commented Jun 3, 2019

Same day renders several times when loading earlier messages. Аnimation below describes the problem.

Actual behavior

wrong

Expected behavior

correct

Code to reproduce

import React, {Component} from 'react';
import {View} from 'react-native';
import {GiftedChat} from 'react-native-gifted-chat';
import moment from 'moment';

export default class ChatApp extends Component {

    constructor() {
        super();

        // Generating some message history.
        this.data = [];
        let createdAt = moment()

        for (let i = 0; i < 100; i++) {
            // We need four message per one day.
            if (!(i / 2 % 2)) {
                createdAt = createdAt.clone().subtract(1, 'day')
            }

            this.data.push({
                _id: i,
                text: i % 2 ? 'Namaste!' : 'Hey!',
                createdAt,
                user: {
                    _id: i % 2 ? 1 : 2,
                    name: i % 2 ? 'Me' : 'You'
                }
            })            
        }

        this.state = {
            messages: this.data.splice(0, 2)
        }
    }

    loadEarlier() {
        // Get two more messages.
        this.setState({
            messages: GiftedChat.prepend(this.state.messages, this.data.splice(0, 2))
        })
    }

    render() {
        return (
            <View style={{flex: 1, backgroundColor: 'white'}}>
                <GiftedChat
                    messages={this.state.messages}
                    loadEarlier={true}
                    onLoadEarlier={() => this.loadEarlier()}
                    renderUsernameOnMessage={true}
                    user={{_id: 1}}
                    renderAvatar={null}
                    placeholder='message'
                    dateFormat={'LL'}
                    inverted={true}
                />
            </View>
        )
    }
}

@xcarpentier xcarpentier merged commit 8180d60 into FaridSafi:master Jun 11, 2019

xcarpentier added a commit that referenced this pull request Jun 13, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.