0

I read QA in stackoverflow, but not described about how to setup controlled single and multiple in same time,

i have a multiple filter, but sometime use can select only one or more, when we come to our react form

  handleChange = (e) => {

    const target = e.target;
    const value = target.value;
    const name  = target.name;
    this.setState({[name]:value})
    this.props.onChange({[name]:value})
  }

..................

carPrice: 
<input 
name="carPrice" 
type="text"  
value={this.state.carPrice} 
onChange={this.handleChange}/> 

price_category : 
<select 
name="price_category" 
value={this.state.price_category} 
onChange={this.handleChange} >
    <option >Select</option>
    <option value="low">Low</option>
    <option value="medium">Medium</option>
    <option value="high">High</option>
</select>

Here is the form, user can select one or more filter same time. the output looks like when user give input

{price_category: "low"}

if give car Price

{ carPrice: "20000"}

Filter here

Here we are filter the sate,

f = (filterparams)=>{

    //filterparams ---> {price_category: "low"} 

    let filtercars = this.state.cars // car array here


    filtercars = filtercars.filter
    (
        car=> {return car.carPrice <= filterparams.carPrice || 
        car.price_category === filterparams.price_category }
    )

      this.setState({
        filtercars
    })
  }

Issue

  • selecting car rate 5000, its show car under the price = work fine
  • then selecting price category high, shows car under 5000 and high = work fine
  • then selecting car rate 1000, shows all cars from price category high = not working

Edit

  render() {
    return (
      <div>
        <CarResult cars={this.state.filtercars} onChange={this.f}/>
      </div>
    )
      }
7
  • Please be specific. Include the code where you think problem lies. At least where f(filterparams) is used. Commented Jul 5, 2019 at 5:05
  • 1
    first i select car price 100. its shows all the result from under 100 price, then select category low, then we will get under 100 & low category cars.... Then again i select car price 50 its shows car price under 50, not considering prev set price category low Commented Jul 5, 2019 at 5:12
  • Now that needs us to see CarResult Component. but I understood that your category is reset every time you set a price. just check your priceHandler and set the category there too Commented Jul 5, 2019 at 5:18
  • 1
    i cant follow you , i am newbie in react, so please provide me a article or code :( Commented Jul 5, 2019 at 5:27
  • 1
    sure, sir please give 2 min Commented Jul 5, 2019 at 5:42

2 Answers 2

1

Here is the CodeSandBox for setting up multiple filters on items in react list. Created to answer this question after the Discussion.

Tips for similar problems

  1. Keep your list/array and filter variables in the parent component
  2. Filter the list in render(){}, don't setState just pass as prop to child component
  3. Pass this.HandleChange as prop
Sign up to request clarification or add additional context in comments.

Comments

0

Can you be more specific with your question?

As per my understanding, looks like its working as you have programmed it. If you have already set category state to high and only changing price to 1000 then its should show all cars from category high under 1000.

If you don't want that to happen you have to reset category state each time you change the price

3 Comments

first i select car price 100. its shows all the result from under 100 price, then select category low, then we will get under 100 & low category cars.... Then again i select car price 50 its shows car price under 50, not considering prev set price category low
Check the state of your react component when you are changing it. You can check with react-dev-tools in chrome browser, or can log the state in render and change method after it gets change. Check if it is updating both of the states. As per your code it should update one state whichever is changed. Also check your filter function if it is in parent component you are only providing the latest change state and not both of your states. If in the same component, you can always add console to check the states getting passed.
i cant follow you , i am newbie in react, so please provide me a article or code :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.