Variable value inaccessible
Answered
Champagne D’Argent posted this in #help-forum
Champagne D’ArgentOP
const AddressPage: NextPage = () => {
const [isSidebarShow, setSidebarShow] = useState(false);
const [TxsData, setTxsData] = useState([]);
const router = useRouter();
const dispatch = useDispatch();
const [Txslimit, setTxsLimit] = useState(10);
const address = router.query.address as string;
console.log("en/Address/{address} :", address)
console.log(Txslimit)
const fetchData = async (address: string) => {
try {
console.log("address?", address)
const response = await axios.post(
`https://ergonode-api.ergohost.io/blockchain/transaction/byAddress?offset=0&limit=${Txslimit}`,
{address}
);
// Assuming the API returns an object with a "data" property
const txsData = response.data.data;
console.log(txsData)
setTxsData(txsData)
// Dispatch the data to your Redux store
// dispatch(setTxs(txsData));
} catch (error) {
console.error("Error fetching data:", error);
}
};
useEffect(() => {
fetchData(address);
}, [Txslimit]); I am working with the Pages Router and I am rending data from an api. I was wondering why the value for address is undefined when useEffect is called
Answered by Ray
useEffect(() => {
if (address) fetchData(address);
}, [address, Txslimit]);try this maybe
30 Replies
Champagne D’ArgentOP
Address seems to be undefined even though const address = router.query.address as string; is valid. Address gets the value. However, I believe the useEffect runs before the value of address is assigned
Champagne D’ArgentOP
@Ray When I refresh there are multiple calls?
useEffect will run twice in react dev mode
it will only run once in production build
Champagne D’ArgentOP
will this reset my useState value
what value?
Champagne D’ArgentOP
I am setting transaction data
using setTxsData()
it will set twice
Champagne D’ArgentOP
I am unsure why it says bad request for the axios, even though I am providing all the required data
that's something related to your backend
im not sure
Champagne D’ArgentOP
I am hitting a public api
I am a bit unsure why that is happening as it says it is a bad request. However, It still returns data
useEffect(() => {
if (address) fetchData(address);
}, [address, Txslimit]);try this maybe
Answer
Champagne D’ArgentOP
That solve's it
I guess it was first calling the api on mount when the variable was defined and once when it was reassigned?
yea
Champagne D’ArgentOP
I am getting a hydration error. Btw how do you know so much about nextjs? do you work for them
I was adding some new components to a template and it said that the data does not match what is rendered on the serverside
Champagne D’ArgentOP
import TableHeader from "./TableHeader";
import TableListItem from "./TableListItem";
import { tableData } from "./helper";
import React from "react";
interface TableWrapperProps{
TxsData: [];
}
const TableWrapper: React.FC<TableWrapperProps> = ({TxsData}) => {
return (
<div className="w-full max-w-[1360px] mx-auto my-6 px-3 sm:px-4 2xl:px-0">
<div className="w-full overflow-auto">
<table className="transactions-table my-8 font-inter text-[14px]">
<TableHeader />
<tbody>
{/* {!TxsData && <p>Loading</p>} */}
{tableData.map((obj, index) => (
<TableListItem obj={obj} index={index} key={index} />
))}
</tbody>
</table>
</div>
</div>
);
};
export default TableWrapper;
import TableListItem from "./TableListItem";
import { tableData } from "./helper";
import React from "react";
interface TableWrapperProps{
TxsData: [];
}
const TableWrapper: React.FC<TableWrapperProps> = ({TxsData}) => {
return (
<div className="w-full max-w-[1360px] mx-auto my-6 px-3 sm:px-4 2xl:px-0">
<div className="w-full overflow-auto">
<table className="transactions-table my-8 font-inter text-[14px]">
<TableHeader />
<tbody>
{/* {!TxsData && <p>Loading</p>} */}
{tableData.map((obj, index) => (
<TableListItem obj={obj} index={index} key={index} />
))}
</tbody>
</table>
</div>
</div>
);
};
export default TableWrapper;
@Ray no i don't work for them
Champagne D’ArgentOP
Damn! how can I get as good as you? You know this like the back of your hand
Do you have a github?
Champagne D’ArgentOP
@Champagne D’Argent Click to see attachment
try this
import TableHeader from "./TableHeader";
import TableListItem from "./TableListItem";
import { tableData } from "./helper";
import React from "react";
interface TableWrapperProps{
TxsData: [];
}
const TableWrapper: React.FC<TableWrapperProps> = ({TxsData}) => {
if (!TxsData.length) return null
return (
<div className="w-full max-w-[1360px] mx-auto my-6 px-3 sm:px-4 2xl:px-0">
<div className="w-full overflow-auto">
<table className="transactions-table my-8 font-inter text-[14px]">
<TableHeader />
<tbody>
{/* {!TxsData && <p>Loading</p>} */}
{tableData.map((obj, index) => (
<TableListItem obj={obj} index={index} key={index} />
))}
</tbody>
</table>
</div>
</div>
);
};
export default TableWrapper;