0

I have state in React functional component. It is and array of objects. Every object in that collection has property "selected", which is a boolean. That array looks like this:

const [filterOptions, setFilterOptions] = useState([
        {
            title: 'highest',
            selected: true,
        },
        {
            title: 'lowest',
            selected: false,
        },
    ]);

After handleFilter func is executed I need to set state so this array has same title properties but reverse (toggle) selected properties.

This is handleFilter func in which I need to toggle every selected property of array objects:

const handleFilter = () => {
        setFilterOptions();
    };
1
  • what's the output you want? could not understand from your post Commented Apr 9, 2020 at 12:50

3 Answers 3

1
function App() {

    const [filterOptions, setFilterOptions] = useState([
        {
            title: 'highest',
            selected: true,
        },
        {
            title: 'lowest',
            selected: false,
        },
    ]);

    const handleFilter = (e) => {
        let newArr = [...filterOptions];

        let value = e.target.value;

        if (value === "lowest") {

            newArr[0].selected = true;
            newArr[1].selected = false;

        } else if (value === "highest") {

            newArr[0].selected = false;
            newArr[1].selected = true;

        } 

        setFilterOptions(newArr)

    };
    return (
        <div>
            <select onChange={handleFilter}>
                <option value="lowest">a</option>
                <option value="highest">b</option>
            </select>
            {console.log((filterOptions))}
        </div>
    );
}
Sign up to request clarification or add additional context in comments.

1 Comment

I dont see how is this solution toggle selected boolean property? I guess you mean !value
0

please check hope it will work

var arryObj =[
        {
            title: 'highest',
            selected: true,
        },
        {
            title: 'lowest',
            selected: false,
        },
    ]

const handleFilter = (index,value) => {
    arryObj[index].selected = value
    };
handleFilter(0,false)
console.log(arryObj)
handleFilter(1,true)
console.log(arryObj)

2 Comments

But what if I don't know the number of objects in that array? I see here you hardcoded calls to handleFilter with indexes 0 and 1? And this needs to be set into state
yes hardcode just for example, hope you will use map for rendering this items , so then you can pass map index and item.selected with sign ! like this !item.selected its means is item.selected = true and passing !item.selected like this it will update your Object example filterOptions.map((item,index)=>( ///*UI here and function on click like this this.handleFilter(index, !item.selected)*//// )) thanks :-)
0

You can pass a function into setFilterOptions to change the state based on the previous state.

const handleFilter = () => {
    setFilterOptions(prevState => 
        prevState.map(obj => ({...obj, selected: !obj.selected}))
    );
};

2 Comments

For this solution I get eslint error "Declaration or statement expected"
@Pokreniseweb My bad, I forgot a pair of parentheses hahaha. Fixed it.

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.