score:2
there must be something wrong with code you didn't post because i can reproduce the code you posted and it's working fine.
note that token is in context.token.token
const authcontext = react.createcontext();
const authprovider = ({ children }) => {
const [token, settoken] = react.usestate({
token: '',
});
const addtoken = token => settoken({ token });
return (
<authcontext.provider
value={{
token,
addtoken,
}}
>
{children}
</authcontext.provider>
);
};
class componentone extends react.component {
constructor(props) {
super(props);
promise.resolve().then(() =>
this.context.addtoken('hello world')
);
}
render() {
return <div>in one: {json.stringify(this.context.token)}</div>;
}
}
componentone.contexttype = authcontext;
const componenttwo = () => {
const { token } = react.usecontext(authcontext);
return <pre>in two: {json.stringify(token)}</pre>;
};
const app = () => (
<authprovider>
<componentone />
<componenttwo />
</authprovider>
);
reactdom.render(<app />, document.getelementbyid('root'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.4/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.4/umd/react-dom.production.min.js"></script>
<div id="root"></div>
Source: stackoverflow.com
Related Query
- How to make a Context API inside an Axios Call
- how do I make axios API call in a separate component / file?
- How can I use an axios get call inside a Context provider in react
- how to make api call from external file using axios and callbacks and pass param in react
- How to fetch API data from Axios inside the getServerSideProps function in NextJS?
- How to make the API call and display it in React JS using React Table and Axios?
- Call web api method inside ReactJS web worker with axios
- How to make a second API call based on the first response?
- How to use "this" keyword inside a fetch API call result
- How to make synchronous API call request in react js
- How do I call a Prismic API inside next.js getInitialProps?
- How to await a setState call finishing when using useState hook that immediately needs that state to make an API call?
- How to make a multiple api call on submission of a form in react
- How can I make a request to an API based on the response from another request? React Hook "useSwr" cannot be called inside a callback
- How to make direct api call to cloudinary with axios?
- How to make common API call function using fetch
- How to use 'useParams()' inside of axios API Call?
- How to make aspecific API call from within a Line Chart class in ReactJS using react-chartjs-2?
- How do I make an API call using createSlice
- How to make an API call on props change?
- How to set multiple states using one response from axios API call in React
- How can I make an api call dynamic with react?
- How to call API inside switch case
- How to make an API call in React on state change?
- When using aws amplify on react to make an call to api gateway, how do i go about getting the statuscode as well?
- How to perform async axios call to REST api and update the store (redux thunk is confusing)?
- How to make api call in react function based view
- Failed with code 405 when using axios to make a call to controller api in asp.net core react application
- How to make api call with react-redux in functional components
- How to automatically call an api before each api call in axios in react
More Query from same tag
- Changing the prop value of embedded component
- React Hooks trying to useState in array ref
- Edit Functionality React
- Try to display data using Mobx from API
- elements not styling properly to the right responsive bootstrap
- How to prevent re render of a component on change of a particular prop?
- How to embed Calendly Anchor Tag With React and Material-UI
- How to create a custom buttons grid in Bootstrap 4?
- Placeholder for images before they are loaded
- Get Base64 String of Ant Design Multi Upload list
- How to add data- attributes in Checkbox component via inputProps
- Does javascript keep a reference to a variable found in a array?
- React functional parent component sharing common onChange functions without redundant code
- Is there any way to import JavaScript function to React component outside from src folder
- Relation between import and destructing es6 syntax?
- Resolve "Can't perform a React state update on an unmounted component" Error
- SVGs don't show on Firefox
- React Router this.props.location is always Undefined
- TypeScript types for passing a React component to React.cloneElement
- React FormControl onChange not being triggered
- Set checkbox value in React JS
- Unit test for attribute fails, only meant to be run on a single node?
- Redux, access data inside API object with useSelector
- Building a SQlite/PouchDB supporting React Web App and questions?
- Why is my background styling getting ignored?
- React-sortable-hoc transition doesnt work
- Shuffle Array click handler event help React
- How to mock redux actions with Jest
- Webpack split scss into separate css files
- Failed prop type: The prop `location` is marked as required in `PrivateRoute`, but its value is `undefined`