Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.4k views
in Technique[技术] by (71.8m points)

javascript - How to filter my search results by location and how to rerender?

I am currently rendering user's search results in my component and those results are being stored in a state called

const [searchData, setSearchData] = useState<any>([]); 

However I've added a left navbar that user can search by location, date posted, etc.... I've managed to get the list of cities(location) of the search results on the left navbar, and if user wanted to click each city I wanted to change the search results filtering only by that city.

I so far have been able to get the console.log(location) of each onClick so now I am stuck how I should re render the results. If I update setSearchData with this onClick handle. I get an undefined .map error of other functions in this component.

So.. How am I suppose to handle re-rendering search results based on clicking location filter?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Please try doing this:

const [searchData, setSearchData] = useState<any>({data: [], save: []}); 

const onChange = (value: string) => {
   setSearchData({
       ...searchData,
       save: searchData.data.filter((x) x.name.indexOf(value) > -1)
   })
}

const state = ["Fish","Dog","Cat"]

const data = state.filter((x) => x.indexOf("Fish") > -1)
console.log(data)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...