I have a simple functional component in React, that renders the search results. If there are results, it renders the names of users, if there are no results, it renders a message no public records found. The problem is that the message is rendered when the component is loaded, whereas I want to show it only when there are no results in serp array. I don't want to change it into class component just because of that, and I'm sure there's a way to "skip" the rendering of the message when the component loads.
Could you help figure it out?
import React from "react";
import { withRouter } from "react-router-dom";
import { connect } from "react-redux";
import PropTypes from "prop-types";
const SearchResults = ({ auth: { user }, search: { users, loading } }) => {
let serp;
if (users.length !== 0) {
serp = users.map((item, i) => {
return <div key={i}>{item.name}</div>;
});
} else {
return <div>no public record found</div>;
}
return <div className="dashboard-container">{serp}</div>;
};
SearchResults.propTypes = {
auth: PropTypes.object.isRequired,
search: PropTypes.object.isRequired
};
const mapStateToProps = state => ({
auth: state.auth,
search: state.search
});
export default connect(mapStateToProps)(withRouter(SearchResults));