1

I have a modal in which a user selects to see more similar (lets say for simplicity products). If there is none, I set state to false and a noresults modal component is shown. the problem is when user AGAIN selects and there is again no products, the boolean is already set to false from before, so since no different value is provided for usestate, the component doesnt re render. However I want to show the modal popup with no results info every time. How can I make the component re render without using bad practises such a force update etc? Thank you

const App = ({route}) => {
    const [more, setMore] = useState(true);
    
    const findItem = (id) => { //triggered from a popup
      const res =  API.find...
        if (res) {
            setMore(true)
        } else {
            setMore(false)
        }
    }
    return (
        {!more ? <NoResults /> : <Results />}
    )
}
  • 1
    Does this answer your question? [Can you force a React component to rerender without calling setState?](https://stackoverflow.com/questions/30626030/can-you-force-a-react-component-to-rerender-without-calling-setstate) – Ahmet Firat Keler Jan 19 '23 at 10:22
  • you must have something that handles the modal close. try setting some state from there. not sure if this is what you actually want to achieve – paperskyline Jan 19 '23 at 10:24
  • I am setting the state kind of from there /trigger it. the problem is that a component with all its children will not re render if value is same so if i call setstate with twice boolean of false, the second time it wont cause re render and i need a re render –  Jan 19 '23 at 10:32
  • Ahmet Firat Keler - the suggested post i already saw, but it suggests forceupdate which is not good practise so i was wondering if there is a different way –  Jan 19 '23 at 10:34
  • What about storing the `productId` with the `more` state together? Like `const [currentProduct, setCurrentProduct] = useState();` with the value being something like `{productId: 123, more: true}` – Thijs Jan 19 '23 at 14:04

0 Answers0