How to show message when filtered list is empty in React












1















I am working on a project in which I am trying to show a div of content that says No results found for if the user types letters in the search input that do not match any filter in the list. I've tried using this similar solution as reference: React: How to show message when result is zero in react, but without success.



Here is a snippet of my code and one solution (of many) I have tried so far:



class App extends React.Component {
constructor(props) {
super(props);

this.state = {
searchQuery: ""
};
}

handleSearchQuery = event => {
this.setState({ searchQuery: event.target.value });
};

resetInputField = () => {
this.setState({ searchQuery: "" });
};

render() {
const { subContent, type, options, label } = this.props;
const { searchQuery } = this.state;
return (
<div
style={{
display: "grid",
alignItems: "center",
width: "100%",
margin: "0 0 24px 0",
fontSize: "14px"
}}
>
<div style={sx.rangeInputContainer}>
<input
style={sx.rangeInputLong}
type="text"
placeholder={placeholderText}
onChange={this.handleSearchQuery}
value={searchQuery}
/>
</div>
<div>
{options
.filter(
option =>
option.label
.toLowerCase()
.includes(searchQuery.toLowerCase()) || !searchQuery
)
.map((option, index) => {
return option.label.length !== 0 ? (
<div key={index} style={sx.filterOption}>
<SquareCheckbox
type="checkbox"
id={"multiSelectCheckbox-" + option.label}
/>
<label
style={{ color: "#FFF" }}
htmlFor={"multiSelectCheckbox-" + option.label}
>
{option.label}
</label>
</div>
) : (
<div
key={index}
style={{
display: "flex",
alignItems: "center",
marginTop: "16px"
}}
>
<img
style={{ width: "20px", cursor: "pointer" }}
src={resetIconSVG}
onClick={this.resetInputField}
/>
<div style={{ marginLeft: "16px" }}>
No results found for {searchQuery}
</div>
</div>
);
})}
</div>
</div>
);
}
}


Here's a snippet of options, which is in my parent component:



this.state = {
filters: [


{
label: 'Materials',
type: FILTER_TYPE.MULTI_SELECT,
expandedHandle: ()=> {
this.handleExpandedToggle('Materials'); },
options:materials,
expanded:false,
},
{
label: 'Status',
type: FILTER_TYPE.SELECT,
expandedHandle: ()=> { this.handleExpandedToggle('Status');
},
options: status,
expanded:false,
},


],
};


And the dummy .json data I am using:



export const materials = [
{ value: 'brass', label: 'brass' },
{ value: 'chrome', label: 'chrome' },
{ value: 'ceramic', label: 'ceramic' },
{ value: 'glass', label: 'glass' },
{ value: 'concrete', label: 'concrete' },

];

export const status = [
{ value: 'Show All', label: 'Show All' },
{ value: 'Enabled Only', label: 'Enabled Only' },

];









share|improve this question




















  • 1





    "but without success" Can you be more specific? Is there an error message? Do you get an unexpected result?

    – Damon
    Nov 13 '18 at 13:55











  • please provide a sample data of this.props.options

    – Nguyễn Thanh Tú
    Nov 13 '18 at 14:04











  • I actually get no response. However if I start the the ternary expression with === 0, then I see the message No results found for repeated four times

    – ShowstopperCode1
    Nov 13 '18 at 14:07











  • @NguyễnThanhTú I've updated the question with a snippet of my options and dummy json data I am using

    – ShowstopperCode1
    Nov 13 '18 at 14:18


















1















I am working on a project in which I am trying to show a div of content that says No results found for if the user types letters in the search input that do not match any filter in the list. I've tried using this similar solution as reference: React: How to show message when result is zero in react, but without success.



Here is a snippet of my code and one solution (of many) I have tried so far:



class App extends React.Component {
constructor(props) {
super(props);

this.state = {
searchQuery: ""
};
}

handleSearchQuery = event => {
this.setState({ searchQuery: event.target.value });
};

resetInputField = () => {
this.setState({ searchQuery: "" });
};

render() {
const { subContent, type, options, label } = this.props;
const { searchQuery } = this.state;
return (
<div
style={{
display: "grid",
alignItems: "center",
width: "100%",
margin: "0 0 24px 0",
fontSize: "14px"
}}
>
<div style={sx.rangeInputContainer}>
<input
style={sx.rangeInputLong}
type="text"
placeholder={placeholderText}
onChange={this.handleSearchQuery}
value={searchQuery}
/>
</div>
<div>
{options
.filter(
option =>
option.label
.toLowerCase()
.includes(searchQuery.toLowerCase()) || !searchQuery
)
.map((option, index) => {
return option.label.length !== 0 ? (
<div key={index} style={sx.filterOption}>
<SquareCheckbox
type="checkbox"
id={"multiSelectCheckbox-" + option.label}
/>
<label
style={{ color: "#FFF" }}
htmlFor={"multiSelectCheckbox-" + option.label}
>
{option.label}
</label>
</div>
) : (
<div
key={index}
style={{
display: "flex",
alignItems: "center",
marginTop: "16px"
}}
>
<img
style={{ width: "20px", cursor: "pointer" }}
src={resetIconSVG}
onClick={this.resetInputField}
/>
<div style={{ marginLeft: "16px" }}>
No results found for {searchQuery}
</div>
</div>
);
})}
</div>
</div>
);
}
}


Here's a snippet of options, which is in my parent component:



this.state = {
filters: [


{
label: 'Materials',
type: FILTER_TYPE.MULTI_SELECT,
expandedHandle: ()=> {
this.handleExpandedToggle('Materials'); },
options:materials,
expanded:false,
},
{
label: 'Status',
type: FILTER_TYPE.SELECT,
expandedHandle: ()=> { this.handleExpandedToggle('Status');
},
options: status,
expanded:false,
},


],
};


And the dummy .json data I am using:



export const materials = [
{ value: 'brass', label: 'brass' },
{ value: 'chrome', label: 'chrome' },
{ value: 'ceramic', label: 'ceramic' },
{ value: 'glass', label: 'glass' },
{ value: 'concrete', label: 'concrete' },

];

export const status = [
{ value: 'Show All', label: 'Show All' },
{ value: 'Enabled Only', label: 'Enabled Only' },

];









share|improve this question




















  • 1





    "but without success" Can you be more specific? Is there an error message? Do you get an unexpected result?

    – Damon
    Nov 13 '18 at 13:55











  • please provide a sample data of this.props.options

    – Nguyễn Thanh Tú
    Nov 13 '18 at 14:04











  • I actually get no response. However if I start the the ternary expression with === 0, then I see the message No results found for repeated four times

    – ShowstopperCode1
    Nov 13 '18 at 14:07











  • @NguyễnThanhTú I've updated the question with a snippet of my options and dummy json data I am using

    – ShowstopperCode1
    Nov 13 '18 at 14:18
















1












1








1








I am working on a project in which I am trying to show a div of content that says No results found for if the user types letters in the search input that do not match any filter in the list. I've tried using this similar solution as reference: React: How to show message when result is zero in react, but without success.



Here is a snippet of my code and one solution (of many) I have tried so far:



class App extends React.Component {
constructor(props) {
super(props);

this.state = {
searchQuery: ""
};
}

handleSearchQuery = event => {
this.setState({ searchQuery: event.target.value });
};

resetInputField = () => {
this.setState({ searchQuery: "" });
};

render() {
const { subContent, type, options, label } = this.props;
const { searchQuery } = this.state;
return (
<div
style={{
display: "grid",
alignItems: "center",
width: "100%",
margin: "0 0 24px 0",
fontSize: "14px"
}}
>
<div style={sx.rangeInputContainer}>
<input
style={sx.rangeInputLong}
type="text"
placeholder={placeholderText}
onChange={this.handleSearchQuery}
value={searchQuery}
/>
</div>
<div>
{options
.filter(
option =>
option.label
.toLowerCase()
.includes(searchQuery.toLowerCase()) || !searchQuery
)
.map((option, index) => {
return option.label.length !== 0 ? (
<div key={index} style={sx.filterOption}>
<SquareCheckbox
type="checkbox"
id={"multiSelectCheckbox-" + option.label}
/>
<label
style={{ color: "#FFF" }}
htmlFor={"multiSelectCheckbox-" + option.label}
>
{option.label}
</label>
</div>
) : (
<div
key={index}
style={{
display: "flex",
alignItems: "center",
marginTop: "16px"
}}
>
<img
style={{ width: "20px", cursor: "pointer" }}
src={resetIconSVG}
onClick={this.resetInputField}
/>
<div style={{ marginLeft: "16px" }}>
No results found for {searchQuery}
</div>
</div>
);
})}
</div>
</div>
);
}
}


Here's a snippet of options, which is in my parent component:



this.state = {
filters: [


{
label: 'Materials',
type: FILTER_TYPE.MULTI_SELECT,
expandedHandle: ()=> {
this.handleExpandedToggle('Materials'); },
options:materials,
expanded:false,
},
{
label: 'Status',
type: FILTER_TYPE.SELECT,
expandedHandle: ()=> { this.handleExpandedToggle('Status');
},
options: status,
expanded:false,
},


],
};


And the dummy .json data I am using:



export const materials = [
{ value: 'brass', label: 'brass' },
{ value: 'chrome', label: 'chrome' },
{ value: 'ceramic', label: 'ceramic' },
{ value: 'glass', label: 'glass' },
{ value: 'concrete', label: 'concrete' },

];

export const status = [
{ value: 'Show All', label: 'Show All' },
{ value: 'Enabled Only', label: 'Enabled Only' },

];









share|improve this question
















I am working on a project in which I am trying to show a div of content that says No results found for if the user types letters in the search input that do not match any filter in the list. I've tried using this similar solution as reference: React: How to show message when result is zero in react, but without success.



Here is a snippet of my code and one solution (of many) I have tried so far:



class App extends React.Component {
constructor(props) {
super(props);

this.state = {
searchQuery: ""
};
}

handleSearchQuery = event => {
this.setState({ searchQuery: event.target.value });
};

resetInputField = () => {
this.setState({ searchQuery: "" });
};

render() {
const { subContent, type, options, label } = this.props;
const { searchQuery } = this.state;
return (
<div
style={{
display: "grid",
alignItems: "center",
width: "100%",
margin: "0 0 24px 0",
fontSize: "14px"
}}
>
<div style={sx.rangeInputContainer}>
<input
style={sx.rangeInputLong}
type="text"
placeholder={placeholderText}
onChange={this.handleSearchQuery}
value={searchQuery}
/>
</div>
<div>
{options
.filter(
option =>
option.label
.toLowerCase()
.includes(searchQuery.toLowerCase()) || !searchQuery
)
.map((option, index) => {
return option.label.length !== 0 ? (
<div key={index} style={sx.filterOption}>
<SquareCheckbox
type="checkbox"
id={"multiSelectCheckbox-" + option.label}
/>
<label
style={{ color: "#FFF" }}
htmlFor={"multiSelectCheckbox-" + option.label}
>
{option.label}
</label>
</div>
) : (
<div
key={index}
style={{
display: "flex",
alignItems: "center",
marginTop: "16px"
}}
>
<img
style={{ width: "20px", cursor: "pointer" }}
src={resetIconSVG}
onClick={this.resetInputField}
/>
<div style={{ marginLeft: "16px" }}>
No results found for {searchQuery}
</div>
</div>
);
})}
</div>
</div>
);
}
}


Here's a snippet of options, which is in my parent component:



this.state = {
filters: [


{
label: 'Materials',
type: FILTER_TYPE.MULTI_SELECT,
expandedHandle: ()=> {
this.handleExpandedToggle('Materials'); },
options:materials,
expanded:false,
},
{
label: 'Status',
type: FILTER_TYPE.SELECT,
expandedHandle: ()=> { this.handleExpandedToggle('Status');
},
options: status,
expanded:false,
},


],
};


And the dummy .json data I am using:



export const materials = [
{ value: 'brass', label: 'brass' },
{ value: 'chrome', label: 'chrome' },
{ value: 'ceramic', label: 'ceramic' },
{ value: 'glass', label: 'glass' },
{ value: 'concrete', label: 'concrete' },

];

export const status = [
{ value: 'Show All', label: 'Show All' },
{ value: 'Enabled Only', label: 'Enabled Only' },

];






javascript reactjs






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 14:17







ShowstopperCode1

















asked Nov 13 '18 at 13:49









ShowstopperCode1ShowstopperCode1

1301211




1301211








  • 1





    "but without success" Can you be more specific? Is there an error message? Do you get an unexpected result?

    – Damon
    Nov 13 '18 at 13:55











  • please provide a sample data of this.props.options

    – Nguyễn Thanh Tú
    Nov 13 '18 at 14:04











  • I actually get no response. However if I start the the ternary expression with === 0, then I see the message No results found for repeated four times

    – ShowstopperCode1
    Nov 13 '18 at 14:07











  • @NguyễnThanhTú I've updated the question with a snippet of my options and dummy json data I am using

    – ShowstopperCode1
    Nov 13 '18 at 14:18
















  • 1





    "but without success" Can you be more specific? Is there an error message? Do you get an unexpected result?

    – Damon
    Nov 13 '18 at 13:55











  • please provide a sample data of this.props.options

    – Nguyễn Thanh Tú
    Nov 13 '18 at 14:04











  • I actually get no response. However if I start the the ternary expression with === 0, then I see the message No results found for repeated four times

    – ShowstopperCode1
    Nov 13 '18 at 14:07











  • @NguyễnThanhTú I've updated the question with a snippet of my options and dummy json data I am using

    – ShowstopperCode1
    Nov 13 '18 at 14:18










1




1





"but without success" Can you be more specific? Is there an error message? Do you get an unexpected result?

– Damon
Nov 13 '18 at 13:55





"but without success" Can you be more specific? Is there an error message? Do you get an unexpected result?

– Damon
Nov 13 '18 at 13:55













please provide a sample data of this.props.options

– Nguyễn Thanh Tú
Nov 13 '18 at 14:04





please provide a sample data of this.props.options

– Nguyễn Thanh Tú
Nov 13 '18 at 14:04













I actually get no response. However if I start the the ternary expression with === 0, then I see the message No results found for repeated four times

– ShowstopperCode1
Nov 13 '18 at 14:07





I actually get no response. However if I start the the ternary expression with === 0, then I see the message No results found for repeated four times

– ShowstopperCode1
Nov 13 '18 at 14:07













@NguyễnThanhTú I've updated the question with a snippet of my options and dummy json data I am using

– ShowstopperCode1
Nov 13 '18 at 14:18







@NguyễnThanhTú I've updated the question with a snippet of my options and dummy json data I am using

– ShowstopperCode1
Nov 13 '18 at 14:18














2 Answers
2






active

oldest

votes


















1














I've made an assumption about your options data, hopefully this helps (I simplified the codes)






class App extends React.Component {
constructor(props) {
super(props);
this.state = {
searchQuery: ''
};
}
handleSearchQuery = event => {
this.setState({ searchQuery: event.target.value });
};
resetInputField = () => {
this.setState({ searchQuery: '' });
};
render() {
const { searchQuery } = this.state;
const options = [
{ label: 'react' },
{ label: 'angular' },
{ label: 'vue' }
];
const filteredOptions = options.filter(
option =>
option.label.toLowerCase().includes(searchQuery.toLowerCase()) ||
!searchQuery
);
return (
<div>
<div>
<input
type="text"
onChange={this.handleSearchQuery}
value={searchQuery}
/>
</div>
<div>
{filteredOptions.length > 0 ? (
filteredOptions.map((option, index) => {
return <div key={index}>{option.label}</div>;
})
) : (
<div>
No results found for {searchQuery}
</div>
)}
</div>
</div>
);
}
}
ReactDOM.render(<App />, document.getElementById('root'));

<script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<div id="root"></div>








share|improve this answer

































    1














    Seems like your using a ternary operator inside of a return on your filter method. I would put the filter into a variable



    const filteredOptions = options.filter(option => option.label.toLowerCase().includes(searchQuery.toLowerCase()) || !searchQuery).map((option, index) => {
    return option.label.length !== 0 ? <div key={index} style={sx.filterOption}>
    <SquareCheckbox type='checkbox' id={'multiSelectCheckbox-' + option.label} />
    <label style={{ color: '#FFF' }} htmlFor={'multiSelectCheckbox-' + option.label}> {option.label} </label>
    </div> })


    and in your render use the ternary to check the length of the array



    render {
    return (
    {filteredOptions.length > 0 ? filteredOptions : <div style = {{ marginLeft: '16px' }}>No results found for { searchQuery }</div>}
    )
    }





    share|improve this answer

























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53282495%2fhow-to-show-message-when-filtered-list-is-empty-in-react%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      I've made an assumption about your options data, hopefully this helps (I simplified the codes)






      class App extends React.Component {
      constructor(props) {
      super(props);
      this.state = {
      searchQuery: ''
      };
      }
      handleSearchQuery = event => {
      this.setState({ searchQuery: event.target.value });
      };
      resetInputField = () => {
      this.setState({ searchQuery: '' });
      };
      render() {
      const { searchQuery } = this.state;
      const options = [
      { label: 'react' },
      { label: 'angular' },
      { label: 'vue' }
      ];
      const filteredOptions = options.filter(
      option =>
      option.label.toLowerCase().includes(searchQuery.toLowerCase()) ||
      !searchQuery
      );
      return (
      <div>
      <div>
      <input
      type="text"
      onChange={this.handleSearchQuery}
      value={searchQuery}
      />
      </div>
      <div>
      {filteredOptions.length > 0 ? (
      filteredOptions.map((option, index) => {
      return <div key={index}>{option.label}</div>;
      })
      ) : (
      <div>
      No results found for {searchQuery}
      </div>
      )}
      </div>
      </div>
      );
      }
      }
      ReactDOM.render(<App />, document.getElementById('root'));

      <script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
      <script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
      <div id="root"></div>








      share|improve this answer






























        1














        I've made an assumption about your options data, hopefully this helps (I simplified the codes)






        class App extends React.Component {
        constructor(props) {
        super(props);
        this.state = {
        searchQuery: ''
        };
        }
        handleSearchQuery = event => {
        this.setState({ searchQuery: event.target.value });
        };
        resetInputField = () => {
        this.setState({ searchQuery: '' });
        };
        render() {
        const { searchQuery } = this.state;
        const options = [
        { label: 'react' },
        { label: 'angular' },
        { label: 'vue' }
        ];
        const filteredOptions = options.filter(
        option =>
        option.label.toLowerCase().includes(searchQuery.toLowerCase()) ||
        !searchQuery
        );
        return (
        <div>
        <div>
        <input
        type="text"
        onChange={this.handleSearchQuery}
        value={searchQuery}
        />
        </div>
        <div>
        {filteredOptions.length > 0 ? (
        filteredOptions.map((option, index) => {
        return <div key={index}>{option.label}</div>;
        })
        ) : (
        <div>
        No results found for {searchQuery}
        </div>
        )}
        </div>
        </div>
        );
        }
        }
        ReactDOM.render(<App />, document.getElementById('root'));

        <script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
        <script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
        <div id="root"></div>








        share|improve this answer




























          1












          1








          1







          I've made an assumption about your options data, hopefully this helps (I simplified the codes)






          class App extends React.Component {
          constructor(props) {
          super(props);
          this.state = {
          searchQuery: ''
          };
          }
          handleSearchQuery = event => {
          this.setState({ searchQuery: event.target.value });
          };
          resetInputField = () => {
          this.setState({ searchQuery: '' });
          };
          render() {
          const { searchQuery } = this.state;
          const options = [
          { label: 'react' },
          { label: 'angular' },
          { label: 'vue' }
          ];
          const filteredOptions = options.filter(
          option =>
          option.label.toLowerCase().includes(searchQuery.toLowerCase()) ||
          !searchQuery
          );
          return (
          <div>
          <div>
          <input
          type="text"
          onChange={this.handleSearchQuery}
          value={searchQuery}
          />
          </div>
          <div>
          {filteredOptions.length > 0 ? (
          filteredOptions.map((option, index) => {
          return <div key={index}>{option.label}</div>;
          })
          ) : (
          <div>
          No results found for {searchQuery}
          </div>
          )}
          </div>
          </div>
          );
          }
          }
          ReactDOM.render(<App />, document.getElementById('root'));

          <script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
          <script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
          <div id="root"></div>








          share|improve this answer















          I've made an assumption about your options data, hopefully this helps (I simplified the codes)






          class App extends React.Component {
          constructor(props) {
          super(props);
          this.state = {
          searchQuery: ''
          };
          }
          handleSearchQuery = event => {
          this.setState({ searchQuery: event.target.value });
          };
          resetInputField = () => {
          this.setState({ searchQuery: '' });
          };
          render() {
          const { searchQuery } = this.state;
          const options = [
          { label: 'react' },
          { label: 'angular' },
          { label: 'vue' }
          ];
          const filteredOptions = options.filter(
          option =>
          option.label.toLowerCase().includes(searchQuery.toLowerCase()) ||
          !searchQuery
          );
          return (
          <div>
          <div>
          <input
          type="text"
          onChange={this.handleSearchQuery}
          value={searchQuery}
          />
          </div>
          <div>
          {filteredOptions.length > 0 ? (
          filteredOptions.map((option, index) => {
          return <div key={index}>{option.label}</div>;
          })
          ) : (
          <div>
          No results found for {searchQuery}
          </div>
          )}
          </div>
          </div>
          );
          }
          }
          ReactDOM.render(<App />, document.getElementById('root'));

          <script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
          <script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
          <div id="root"></div>








          class App extends React.Component {
          constructor(props) {
          super(props);
          this.state = {
          searchQuery: ''
          };
          }
          handleSearchQuery = event => {
          this.setState({ searchQuery: event.target.value });
          };
          resetInputField = () => {
          this.setState({ searchQuery: '' });
          };
          render() {
          const { searchQuery } = this.state;
          const options = [
          { label: 'react' },
          { label: 'angular' },
          { label: 'vue' }
          ];
          const filteredOptions = options.filter(
          option =>
          option.label.toLowerCase().includes(searchQuery.toLowerCase()) ||
          !searchQuery
          );
          return (
          <div>
          <div>
          <input
          type="text"
          onChange={this.handleSearchQuery}
          value={searchQuery}
          />
          </div>
          <div>
          {filteredOptions.length > 0 ? (
          filteredOptions.map((option, index) => {
          return <div key={index}>{option.label}</div>;
          })
          ) : (
          <div>
          No results found for {searchQuery}
          </div>
          )}
          </div>
          </div>
          );
          }
          }
          ReactDOM.render(<App />, document.getElementById('root'));

          <script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
          <script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
          <div id="root"></div>





          class App extends React.Component {
          constructor(props) {
          super(props);
          this.state = {
          searchQuery: ''
          };
          }
          handleSearchQuery = event => {
          this.setState({ searchQuery: event.target.value });
          };
          resetInputField = () => {
          this.setState({ searchQuery: '' });
          };
          render() {
          const { searchQuery } = this.state;
          const options = [
          { label: 'react' },
          { label: 'angular' },
          { label: 'vue' }
          ];
          const filteredOptions = options.filter(
          option =>
          option.label.toLowerCase().includes(searchQuery.toLowerCase()) ||
          !searchQuery
          );
          return (
          <div>
          <div>
          <input
          type="text"
          onChange={this.handleSearchQuery}
          value={searchQuery}
          />
          </div>
          <div>
          {filteredOptions.length > 0 ? (
          filteredOptions.map((option, index) => {
          return <div key={index}>{option.label}</div>;
          })
          ) : (
          <div>
          No results found for {searchQuery}
          </div>
          )}
          </div>
          </div>
          );
          }
          }
          ReactDOM.render(<App />, document.getElementById('root'));

          <script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
          <script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
          <div id="root"></div>






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 13 '18 at 14:22

























          answered Nov 13 '18 at 14:17









          Nguyễn Thanh TúNguyễn Thanh Tú

          4,6943827




          4,6943827

























              1














              Seems like your using a ternary operator inside of a return on your filter method. I would put the filter into a variable



              const filteredOptions = options.filter(option => option.label.toLowerCase().includes(searchQuery.toLowerCase()) || !searchQuery).map((option, index) => {
              return option.label.length !== 0 ? <div key={index} style={sx.filterOption}>
              <SquareCheckbox type='checkbox' id={'multiSelectCheckbox-' + option.label} />
              <label style={{ color: '#FFF' }} htmlFor={'multiSelectCheckbox-' + option.label}> {option.label} </label>
              </div> })


              and in your render use the ternary to check the length of the array



              render {
              return (
              {filteredOptions.length > 0 ? filteredOptions : <div style = {{ marginLeft: '16px' }}>No results found for { searchQuery }</div>}
              )
              }





              share|improve this answer






























                1














                Seems like your using a ternary operator inside of a return on your filter method. I would put the filter into a variable



                const filteredOptions = options.filter(option => option.label.toLowerCase().includes(searchQuery.toLowerCase()) || !searchQuery).map((option, index) => {
                return option.label.length !== 0 ? <div key={index} style={sx.filterOption}>
                <SquareCheckbox type='checkbox' id={'multiSelectCheckbox-' + option.label} />
                <label style={{ color: '#FFF' }} htmlFor={'multiSelectCheckbox-' + option.label}> {option.label} </label>
                </div> })


                and in your render use the ternary to check the length of the array



                render {
                return (
                {filteredOptions.length > 0 ? filteredOptions : <div style = {{ marginLeft: '16px' }}>No results found for { searchQuery }</div>}
                )
                }





                share|improve this answer




























                  1












                  1








                  1







                  Seems like your using a ternary operator inside of a return on your filter method. I would put the filter into a variable



                  const filteredOptions = options.filter(option => option.label.toLowerCase().includes(searchQuery.toLowerCase()) || !searchQuery).map((option, index) => {
                  return option.label.length !== 0 ? <div key={index} style={sx.filterOption}>
                  <SquareCheckbox type='checkbox' id={'multiSelectCheckbox-' + option.label} />
                  <label style={{ color: '#FFF' }} htmlFor={'multiSelectCheckbox-' + option.label}> {option.label} </label>
                  </div> })


                  and in your render use the ternary to check the length of the array



                  render {
                  return (
                  {filteredOptions.length > 0 ? filteredOptions : <div style = {{ marginLeft: '16px' }}>No results found for { searchQuery }</div>}
                  )
                  }





                  share|improve this answer















                  Seems like your using a ternary operator inside of a return on your filter method. I would put the filter into a variable



                  const filteredOptions = options.filter(option => option.label.toLowerCase().includes(searchQuery.toLowerCase()) || !searchQuery).map((option, index) => {
                  return option.label.length !== 0 ? <div key={index} style={sx.filterOption}>
                  <SquareCheckbox type='checkbox' id={'multiSelectCheckbox-' + option.label} />
                  <label style={{ color: '#FFF' }} htmlFor={'multiSelectCheckbox-' + option.label}> {option.label} </label>
                  </div> })


                  and in your render use the ternary to check the length of the array



                  render {
                  return (
                  {filteredOptions.length > 0 ? filteredOptions : <div style = {{ marginLeft: '16px' }}>No results found for { searchQuery }</div>}
                  )
                  }






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 13 '18 at 14:41









                  Caribouflex

                  22827




                  22827










                  answered Nov 13 '18 at 14:04









                  DmitriyDmitriy

                  490212




                  490212






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53282495%2fhow-to-show-message-when-filtered-list-is-empty-in-react%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Florida Star v. B. J. F.

                      Error while running script in elastic search , gateway timeout

                      Adding quotations to stringified JSON object values