score:0
You specify the api url relative to the current page url:
...
const { data } = await axios.get(`api/products/${productId}`);
...
// the api url will change depending on the url of the current page
// http://localhost:3000 -> http://localhost:3000/api/products/${productId}
// http://localhost:3000/product -> http://localhost:3000/product/api/products/${productId}
// http://localhost:3000/about -> http://localhost:3000/about/api/products/${productId}
// ...etc
Just add a leading slash for the api address. It ensures the path is absolute to the root directory and not the current directory
...
const { data } = await axios.get(`/api/products/${productId}`);
...
Source: stackoverflow.com
Related Query
- How can i get a single data from local api using react and redux?
- How can I cache data that I already requested and access it from the store using React and Redux Toolkit
- How do I display data from api using react and redux
- How to concatenate URL and string, and get data from API at click a button using axios in REACT JS
- How can I get data from a local file into my React app?
- How to get data from local json file using actions and axios.get() (Redux)?
- How can I get data from query string using route in react js
- How to Assign react redux state api get data using axios
- How can I create a button which Fetches image from API and download to the local with using ReactJs
- How to get the data from Redux store using class components in React
- How to get data from an api having XML content using react
- React and Express: Using Axios to GET data from 3rd party API
- How to get a single data element from the database with Redux and React?
- How to set state at the server for data from API using react redux
- How can I pass data to an object using React Hooks and Redux
- how can get data from api to child combobox in react
- How can I wait reducer update data from API on event onClick with redux and redux - Saga?
- how to get spesific data from api with axios and react js?
- React - get data from API and show in return using MAP function
- How can I print data from database in flask app using javascript and react components?
- How can I update react app that gets it's data from the node mongo backend using redux
- How to delete data from database by using React js and REST API
- How to send multiple objects to API and GET the result from API using react js?
- Can't render out single page data from list in a react application using router and WordPress API
- How to execute store.unsubscribe from useEffect using React hooks and Redux
- How do I access data returned from an axios get to display on a web page using react js?
- How can I cache data from API to Cache Storage in React PWA?
- React Context : Get Data from API and call API whenever some events happens in React Component
- How to redirect to 404 if no data from external API (universal React + Redux app)?
- How can i get array of object from this data for state and count in reactjs
More Query from same tag
- How to complete login authentication in react by using react query mutations?
- React configureStore2.default.firestore is not a function
- Why is my custom sort not working with Material-UI Data Grid component?
- reactjs useState and useEffect hooks not re-rendering after array data change for data received via websocket
- Set acitve classname to multiple items in react js (map) and remove
- How can I persist updated form data after submit?
- Proptypes children of type component not working
- Scroll to element in React based on "getElementsByName" function
- How to change a button using Redux
- get response status of 415 Unsupported Media Type REST client
- Vertical scroll snap doesn't work with Next.js
- Call an Async action after updating the react state
- change useState when redux state change
- TypeError: Cannot read properties of undefined (reading 'map') Trying to Read REST api and display
- React input or material-ui TextField not working inside material-ui TreeView
- How to detect if screen size has changed to mobile in React?
- Breaking an array items into certain number of rows
- Reactjs: Uncaught (in promise) TypeError: Cannot read property 'data' of undefined
- Firebase.auth().onstateChanged() not working after clearing browser cache
- React.js Firebase and Firepad Component
- Error: Before running a Saga, you must mount the Saga middleware on the Store using applyMiddleware
- Why It’s difficult to remember which props or state are used by functions outside of the effect
- How do I convert this class component into function component?
- Custom chakra theme is slow on load
- Axios.post keeps sending empty objects to my API
- React-player How to handle Play Pause state with autoplay
- Playing audio in a loop with React
- React component State clears after adding additional query parameters to the URL
- How can I get the state value in the reducer of createSlice?
- Pass data from component to action and saga using ARc/React/Redux