2021年3月19日星期五

Is there a way to modify the useState of another function?

I would like to write a function that can be called to change a useState hook, but the state is in a different function.

function Viewer(props) {      const [linesToDisplay, setLinesToDisplay] = useState()        useEffect(() => {            if (props.json != undefined) {              setLinesToDisplay(//set lines based on the json file)            }      }, []      )        function hideIDs(idsToHide) {          var idsToShow = []          linesToDisplay.forEach(line => {              idsToHide.includes(line.id) ? null : idsToShow.push(line)          })          setLinesToDisplay(idsToShow)      }  

Obviously, I can't do this as I've implemented it. But I would like to know if there is some way to do this. If it's not possible, is there better way to implement state setting behaviour?

https://stackoverflow.com/questions/66717487/is-there-a-way-to-modify-the-usestate-of-another-function March 20, 2021 at 09:36AM

没有评论:

发表评论