import React from 'react'
    import FormatUrl from 'utils/UrlFormatter.jsx'

    class SocialChatter extends React.Component {
        constructor(props){
            super(props)
            this.state = {
                chatter : [],
                limit:5,
                page:1,
                totalPages:null,
                channel:null,
                category:'Dish Care'
            }    
        }


        componentDidMount = () => {
            const {limit, page, chatter } = this.state;
            let category = localStorage.getItem('category')
            let url = FormatUrl(`/api/category/social/?category=${category}&limit=${limit}&page=${page}`)        
            fetch(url)
            .then(res => res.json())
            .then(res => {
                this.setState({
                    chatter:res.data,
                    chatter:[...chatter, ...res.data]
                });
            })
        }

        loadMore = () => {
            this.setState(prevState => ({
                page: prevState.page + 1,
            }), () => this.componentDidMount())
        }


        render(){
            return (
                <div>
                    <div class="card shadow mb-4">
                        <div class="card-header py-3 d-flex flex-row align-items-center justify-content-between">
                            <div class="dropdown no-arrow" style={{ padding: "8px 0 8px 0" }}>      
                            <a href="#" style={{ padding:'0 16px 0 0' }}>
                            </a>
                            <a href="#" style={{ padding:'0 16px 0 0' }}> 
                            </a>
                            <a href="#">
                            </a>
                        </div>
                    </div>
                        <div class="card-body">
                            <div class="chart-area">
                            <div className="row">
                                    <div>
                                           {
                                                this.state.chatter.map((chat, i) => {
                                                    return (
                                                    <div className="col-sm-12" key={i}>
                                                        <div class="panel panel-default">
                                                            <div class="panel-heading" style={{ textAlign:'center' }}>
                                                                <a href={ chat.url } target="_blank" rel="noopener noreferrer" style={{ textDecoration:'none' }}>
                                                                    { chat.title }
                                                                </a>
                                                            </div>
                                                            <div class="panel-body">
                                                            { chat.content }
                                                            <br/>
                                                            <p style={{ color:'grey',fontWeight:'bold',float:'right' }}>
                                                                { chat.date }
                                                            </p>
                                                            </div>

                                                        </div>
                                                    </div>
                                                    )
                                                })
                                            }
                                            <p style={{ textAlign:'center' }}><button onClick={this.loadMore} className="btn btn-danger">View More</button></p>
                                    </div>
                            </div>
                        </div>
                    </div>
                </div>
                </div>
            )
        }
    }

    export default SocialChatter;

Here i have fetching data from api. and displaying them here by mapping the json.

But, My componentDidMount() automatically getting called after 5, 6 seconds.

It is calling the prev state automatically after some time.

This issue quite bit confusing i could not find any appropriate sol for this issue.

Please have a look whey it is happening.

0 Answers