'cant set a state inside function (react)
Learning by coding, here i have simple graph, where are different datas such as current, all and filtered data, first code works fine, but i wanted to setState so i changed code a little (second code), if i uncomment 'setTest' it give 'Error: Too many re-renders. React limits the number of renders to prevent an infinite loop.' is there a fix to this or should i somehow use useEffect? and another question is why should i call function 'kk()', and not just reference to it 'kk' because when i use reference it give error 'Uncaught TypeError: Cannot read properties of undefined' but when calling function it wont give error, but then i have used reference to function without problem 'onClickNode={onClickNode}' .
1:
import { Graph } from "react-d3-graph";
<Graph data={
hideData ?
curentData ? allData : filteredData
: allData }
onClickNode={onClickNode}
/>
2:
import { Graph } from "react-d3-graph";
const [test, setTest] = useState<any>("testData1");
const kk = () => {
// setTest("testData2")
return curentData ? allData : filteredData;
};
<Graph
data={hideData ? kk() : allData} onClickNode={onClickNode}
/>
English is not my mother language so could be mistakes.
Solution 1:[1]
You should not call a state changing function within your rendering logic without some sort of condition. Without a condition to prevent a state change results in an infinite loop as you are seeing.
One option you can try is call setTest("testData2") on a particular user action, like this:
const [test, setTest] = useState<any>("testData1");
const kk = () => {
return curentData ? allData : filteredData;
};
const onClickNode = () => {
// Handle other click logic...
setTest("testData2");
};
<Graph data={hideData ? kk() : allData} onClickNode={onClickNode} />
But without seeing more of that code, that option may not make sense for you.
Another option would be to check the value of test before calling setTest("testData2"), like this:
const [test, setTest] = useState<any>("testData1");
const kk = () => {
if (test !== "testData2") {
setTest("testData2");
}
return curentData ? allData : filteredData;
};
<Graph data={hideData ? kk() : allData} onClickNode={onClickNode} />
Solution 2:[2]
Step 1: clone the git repo to a folder (ex. C:\Users\Cam\Desktop\mod-ah-bot) on your local server. Here's the link: https://github.com/azerothcore/mod-ah-bot.git (You must clone the repo, simply downloading it did not work for me)
Step 2: Move the folder you just cloned that repo to into your Azerothcore/Modules folder. The whole folder, throw it in there.
Step 3: Grab the "mod_ahbot.conf.dist" file from the 'conf' folder (mod-ah-bot/conf) and throw it in the same directory as your worldserver.exe.
Step 4: You'll want to modify that file to your preferences. Just change the name to "mod_ahbot.conf", edit the file, save the file, then change the name back to "mod_ahbot.conf.dist"
Step 5: Open cmake (you would've used this before to build your server if you followed the install directions on AzerothCore site, here's the link: https://www.azerothcore.org/wiki/windows-core-installation , specifically the cmake section.
Step 6: Ensure the source code directory (AzerothCore location) is filled in at the top. (ex. C:/users/$user/desktop/azerothcore)
Then fill in the binaries location under that (ex. C:/users/$user/desktop/build)
Step 7: Click configure on the bottom left, then click generate, then click open project.
Step 8: Right click "Solution 'AzerothCore' (26 of 26 projects" located on the left side of VS code and click 'Rebuild Solution'
Step 9: Wait for binaries to compile or whatever magic is happening there.
Step 10: Lets go back to that mod-ah-bot folder we cloned and open the sql folder. Go a few folders deep and you're going to find a .sql file. Run that against your acore_world database.
Step 11: Assuming there were no red flags! Launch your Authserver.exe, then launch your wowserver.exe and at the end of the wowserver.exe cmd window it should say this: "AuctionHouseBot and AuctionHouseBuyer have been loaded." If it does celebrate!! And get to tweaking the mod_auctionhousebot table that sql query we ran created to your liking.
If it didn't, well... yeah..
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | segFault |
| Solution 2 |
