score:0
since you are using react-router push method. it also has the ability to send different props value along with your pathname and this push will work only on components wrapped in .
in your case, you need to pass a state along with your route name. below changes, you have to pass state along with your route name in history.push
.
this.props.history.push("/welcome", { ...res });
on login.js
.
and get the response values on your welcome.js
by using this.props.location.state.firstname
.
attaching the document for your reference - https://github.com/reacttraining/react-router/blob/master/packages/react-router/docs/api/history.md
and also, i attached a basic demonstration of history.push in this - https://codesandbox.io/s/broken-flower-hbglx.
score:0
one possible solution would be to use some state management library such as redux.
otherwise you will need to have a component that will do this fetching stuff and this component should be a common ancestor to the components that will be at least affected with the data changes from api.
my objective is how to pass the the user name while user is getting logged in. when user gets logged in then it should show "welcome username".
as a more generic solution, you can create a hoc that will do the fetching api stuff and return a new component with the data from the api injected as props.
score:4
you can do it like this:
axios
.post(`/api/login/auth`, data, {
headers: { "content-type": "application/json" }
})
.then(res => {
console.log(res);
this.props.history.push("/welcome", { ...res });
})
.catch(err => {
console.log(err.response.data.message);
var res = err.response.data.message;
this.setstate({ errormsg: res });
});
and as you are sending json web token
as data, you can access it as:
this.props.data.item[0].firstname
hope this works for you.
Source: stackoverflow.com
Related Query
- How to pass data from one component to another while using API in reactjs
- How to pass data from one component to another ReactJS
- How to pass an array of data to one component to another component using history.push in ReactJs when fetched using Axios
- How to pass data from one component to another component in onchange using React js
- React - How can I pass API data from one component to another in a different js file?
- How can i pass data from one component to another using hooks
- How to access passed data using router from one component to another ReactJS
- Pass data from one component to another using ReactJS
- How to pass array of data from one component to another component in reactjs
- How to pass json data from one component to another using functions in react
- How to pass data in 'this.state' from one component to another in ReactJs
- How to pass data from one component to another in React or React-Redux?
- Using data from one component in another in Reactjs
- How to pass state/data from one component to another in React.js (riot api specifically)
- ReactJS How to pass iterated values to child component from parent component using Context API
- How to fetch data from API and then pass it to another component in react after it is fully loaded?
- How to pass data received from database as props to another component in Reactjs
- How to pass a prop from one component to another when using react router link
- ReactJs - How to pass particular item id from one component to another component
- how to pass the data from one component to another in react js
- How to pass data from one component to another via a function in ReactJS?
- How can I pass props from one component to another using Link and router in react
- How to transfer data from one parent component to another parent component in react using router / router params
- How to pass method from one component to another without using Arrow Function?
- How do I pass data from one sibling to another using React Router?
- How to pass data from one API request to another with Node
- React couldn't pass a data from one class component into another using Link
- How to pass data from one component to another component/parent in React
- How to pass data value from one Child Component to another Child Component in React?
- How to pass value from one component to another using onClick in React
More Query from same tag
- How would this Angular 10 code be in React?
- How to get the output of a shell command to a React frontend through a Flask RESTful API?
- What is the preferred syntax for spacing out inline child elements with the MUI5 sx prop?
- How to save input entry in a state of component for an API call using componentDidMount and Async?
- How does one unit test Redux set up with Redux Tool Kit's 'createSlice'?
- React-router v5 updates the URL but doesn't render nested component
- React Native: error: resource android:style/TextAppearance.Material.Widget.Button.Borderless.Colored not found
- When I open the modal, the api call running always
- how can i reverse the order of an array with usestate
- How to mock out a response triggered by a function on the same component in a wrapper?
- Where to store JWT token in react / client side in secure way?
- React Router Error Cannot read property 'pathname' of undefined
- Material-UI AutoComplete Grouping logic does not honor duplicates
- React setState error anyone help ? minified error
- How to display value with '%' react chart-race
- Ionic 5 changing grid size and applying style not working with react?
- Awaiting status update from API with React Hooks (useEffect)?
- Having a react jsx component in state, Best practice
- Adding an attribute in a <div> if the attribute has value
- componentDidMount method not triggered when using inherited ES6 react class
- Why can react-test-renderer only findByType functional components in the same file in React?
- React Big Calendar label - get year in week view
- How to display images from api in nextjs
- Set types on useState React Hook with TypeScript
- "cannot read property 'push' of undefined" in recursive function, React
- How get the value of {match.params.id} on react router
- Getting dotted line on html Canvas
- Dialog form constantly reendering elements slowing down my application (and crashing)
- Approach for useEffect to avoid exhaustive-dep warning
- Inject React component into html page