use query results in state

Hi, I want to query my database for routes, and then set the DashboardState with the data from the query result. How can I do this?
interface DashboardState {
isNewRouteModalOpen: boolean;
isEditRouteModalOpen: boolean;
routes: Route[];
}

const initState: DashboardState = {
isNewRouteModalOpen: false,
isEditRouteModalOpen: false,
routes: [],
};

const Dashboard: NextPage = () => {
const [state, setState] = useState(initState);
const routes = api.example.getRoutes.useQuery();
interface DashboardState {
isNewRouteModalOpen: boolean;
isEditRouteModalOpen: boolean;
routes: Route[];
}

const initState: DashboardState = {
isNewRouteModalOpen: false,
isEditRouteModalOpen: false,
routes: [],
};

const Dashboard: NextPage = () => {
const [state, setState] = useState(initState);
const routes = api.example.getRoutes.useQuery();
3 Replies
barry
barry2y ago
onSuccess property of useQuery
thatloudmango
thatloudmangoOP2y ago
thank you
Valhalaar
Valhalaar2y ago
does react query not store the result of a query in state? const routes = api.example.getRoutes.useQuery(); As in, is this not already your state when it's returned
Want results from more Discord servers?
Add your server