ONe more thing is, when I use useState and useEffect to get values from a function - I don’t see the values when I display them.
But when I use it this way, it works:
Why won’t it show me the value of getAllBoards() (this returns an array of objects)?
When I iterate through it and display it with useState and useEffect, it doesn’t work
Hi @mpaisley. I used UI Kit 2. I could resolve my form error. It was a problem with the Button.
But the useState and useEffect never renders the results from my function
The useState should be initialized with a synchronous function.
If you want the state to be updated with async calls, you can either delegate your data fetching to libraries such as react-query, or you can do something like
Awesome, in UI Kit 2 apps, you’re actually using React useState and useEffect rather than the forge implementation - they can work a little differently, especially when working with async functions.
In this case, it looks like you’re not using useState correctly.
I’m assuming what you’re seeing here is that boardnames promise that hasn’t been fulfilled yet. As @PaoloCampanelli mentioned, this needs to be initialized with a synchronous function.
You can use a combination of useState and useEffect here to update the state - Paolo gave a good example above.
Just to be sure, I’d place a console.log before const fetchBoardNames... so that you can check that the useEffect is actually being invoked.
In general you don’t need to define a const functions to specify an effect, you can just do something like
One morething that I can suggest trying is adding setBoardNames and getAllBoards as dependencies for the hook. In React, the setters are considered stable and should never change… but who knows, you can do that by specifying [setBoardNames, getAllBoards] in the second parameter for useEffect
Hey, thanks! That works. One more question is
I’m trying to render the second dropdown values after user selects the first one. So onChange = {handleboardSelect}
In that case you don’t need a separate useEffect: when you’re reacting to an event – in this case onChange – you should generally handle all the side effects in the event handler: it’s easier to follow the flow that way.
Of course, if you have an useEffect that has selectedBoardId in its dependencies, it would work too, but it’s not something I’d personally recommend.
I did this but I don’t think this is populating the sprintnames - and I’m not sure why but I cant console.log the selectedboardId either - it says undefined
Not sure why this doesn’t update the sprint names drop down. I don’t see forge logs either. board data is fetched but selectedBoardId, sprintdata is not showing up at all.
Could you please tell me what I’m doing wrong?
If I had to guess, your handleBoardSelect function is getting value={label: "Your board name", value: your_board_id}, which means that selectedBoard is always going to be null.
You can easily verify that with console.log(value) in handleBoardSelect.
If that’s indeed the case, you can trivially fix that with handleBoardSelect = ({value}) => { ...
Yeah, as I’ve explained, const selectedBoard = boardnames.find((board) => board.boardId === value); is probably null, so the if (selectedBoard) is always false.
You can check that by logging value immediately in handleBoardSelect