0

I want to create searchable input but something went wrong that's why this is not working. How can I fix it?

My code:

import React, {useState} from 'react'; import "./styles.css"; export default function App() { const country_list = [ "Argentina", "Australia", "Belgium", "Belize", "China", "Iceland", "India", "Indonesia", "Iran", "Poland", "Portugal", "Romania", "Russia", "Saudi Arabia", "South Africa", "South Korea", "Swaziland", "Sweden", "Switzerland" ]; const [inputVal, setInputVal] = useState(); country_list.filter(country => { if(inputVal === ''){ return country; } else if (country.toLowerCase().includes(inputVal.toLowerCase())) { return country; } }); return ( <div className="App"> <input type="text" onChange={event => setInputVal(event.target.value)} /> {inputVal} {country_list.map((val, index)=>( <> <div key={index}> {val} </div> </> ))} </div>) }

1
  • You never assign the result of filter to anything Commented Feb 8, 2023 at 9:18

4 Answers 4

1

To make that code work you should fix some strings. I posted comments bellow. Also I recommend move your changable variables like country_list to useState because the state of your app can get out of sync with the view.

import React, { useState } from 'react'; import "./styles.css"; export default function App() { let country_list = [ "Argentina", "Australia", "Belgium", "Belize", "China", "Iceland", "India", "Indonesia", "Iran", "Poland", "Portugal", "Romania", "Russia", "Saudi Arabia", "South Africa", "South Korea", "Swaziland", "Sweden", "Switzerland" ] // change country_list to let so we could reasssign it; const [inputVal, setInputVal] = useState(''); // use empty string in useState as initial state for you inputVal country_list = country_list.filter(country => { if (inputVal === '') { return country; } else if (country.toLowerCase().includes(inputVal.toLowerCase())) { return country; } }); // function filter in JS create new array everytime instead of changing the old one. You should reassign your country_list variable return ( <div className="App"> <input type="text" onChange={event => setInputVal(event.target.value)} /> {inputVal} {country_list.map((val, index) => ( <> <div key={index}> {val} </div> </> ))} </div>) } 
Sign up to request clarification or add additional context in comments.

Comments

1

I want to create searchable input but something went wrong thats why this is not working. How can i fix it?

Your code seems fine, just move it to the return block, before mapping:

 {country_list.filter(country => { if(inputVal === ''){ return country; } else if (country.toLowerCase().includes(inputVal.toLowerCase())) { return country; } }).map((val, index)=>( ... )} 

Comments

0

I believe that using this going to work, move your code to the return () and map the returned value from the filter try this code below and its gonna work :)

{countryList .filter((country) => { if (inputVal == "") { return country; } else if (country.toLowerCase().includes(inputVal.toLowerCase())) { return country; } }) .map((Val, index) => { return ( <div key={index}> {val} </div> ); })} 

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
0

You can do like this if you want data

import React, { useState } from "react"; import "./styles.css"; export default function App() { const country_list = [ "Argentina", "Australia", "Belgium", "Belize", "China", "Iceland", "India", "Indonesia", "Iran", "Poland", "Portugal", "Romania", "Russia", "Saudi Arabia", "South Africa", "South Korea", "Swaziland", "Sweden", "Switzerland" ]; const [inputVal, setInputVal] = useState(" "); const countryList = country_list.filter((country) => { if (inputVal === "") { return country; } else if (country.toLowerCase().includes(inputVal.toLowerCase())) { return country; } return country; }); console.log(countryList); return ( <div className="App"> <input type="text" onChange={(event) => setInputVal(event.target.value)} /> {inputVal} {countryList.map((val, index) => ( <> <div key={index}>{val}</div> </> ))} </div> ); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.