You have a consumer at the top of the tree. This is not how it works. The provider goes to the top. And all you are putting in the context is the file name, not the properties you are trying to use.
The structure of you app is very confusing. Normally app.js would only have routing and common page chrome (layout).
In the provider you set value to a function. So when you use value in the consumer it’s still a function. Normally you would call it in the consumer somewhere. as it a state setter you’d expect it to be used as an event handler. Not sure why you expect the
function to have properties.
You should go thru some tutorials. You appear to be just trying random code samples.
Member
21 Points
246 Posts
What is my mistake in using Context in React?
Feb 22, 2021 02:30 PM|SaeedP|LINK
Hello,
You could see below the process of creating, providing, and consuming context. Which step is wrong that my code doesn't work?
imageContext.js
import React from 'react'; const ImageContext = React.CreateContext('null'); export default ImageContext;
ImageUpload.js
return ( <ImageContext.provider value={this.state.selectedFile.name}> <div> <div style={mystyle} onClick={this.onNavToEditor}/> <h1>File Upload Demo</h1> <div >{this.state.uploadResult} onClick={this.onNavToEditor}</div> <div> <input type="file" onChange={this.onFileChange} /> <input type="text" value={this.state.value} onChange={this.onDescriptionChange} /> <button onClick={this.onFileUpload}> Upload! </button> </div> {this.listItems()} </div> </ImageContext.provider>
ImageEditor.js
thanks,
Saeed
All-Star
58144 Points
15646 Posts
Re: What is my mistake in using Context in React?
Feb 22, 2021 04:24 PM|bruce (sqlwork.com)|LINK
you don't say what is not working. nor do you show any code that sets a value to the context. but odd stuff in your sample code:
setting the default context to the string 'null'. why? everywhere you use it you expect it to be an object
when you set the context value via the provider you use:
this.state.selectedFile.name
which again appears to be a string.
then in the consumer (assuming its a child of the provider, you don't explain how its rendered), you expect the context to an object:
loadImage: { path: context.path, name: context, },
as you do not show how ImageUpload.js state is set or updated, we don't know what value is passed to the ImageContext.Provider prop.
Member
21 Points
246 Posts
Re: What is my mistake in using Context in React?
Feb 22, 2021 07:59 PM|SaeedP|LINK
I mean after clicking the image thumbnail, it isn't being opened in the image editor:
ImageUpload.js
Member
21 Points
246 Posts
Re: What is my mistake in using Context in React?
Feb 22, 2021 08:08 PM|SaeedP|LINK
I mean after clicking the image thumbnail, it isn't being opened in the image editor:
ImageUpload.js
ImageEditor:
ImageEditor.js and ImageUpload.js both of them are components.
All-Star
58144 Points
15646 Posts
Re: What is my mistake in using Context in React?
Feb 23, 2021 03:37 AM|bruce (sqlwork.com)|LINK
You have a consumer at the top of the tree. This is not how it works. The provider goes to the top. And all you are putting in the context is the file name, not the properties you are trying to use.
The structure of you app is very confusing. Normally app.js would only have routing and common page chrome (layout).
Member
21 Points
246 Posts
Re: What is my mistake in using Context in React?
Feb 25, 2021 01:50 PM|SaeedP|LINK
I changed the provider:
Now:
return ( <ImageContext.Provider value={this.onFileChangeName}> </ImageContext.Provider >
And consumer:
return ( <ImageContext.Consumer> {value => ( <div className="home-page"> <div className="center"> <h1>Photo Editor</h1> <Button className='button' onClick={saveImageToDisk}>Save Image to Disk</Button> </div> <ImageEditor includeUI={{ loadImage: { path: value.path, name: value, }, theme: myTheme, menu: ["crop", "flip", "rotate", "draw", "shape", "text", "filter"], initMenu: "", uiSize: { height: `calc(100vh - 160px)`, }, menuBarPosition: "bottom", }} cssMaxHeight={window.innerHeight} cssMaxWidth={window.innerWidth} selectionStyle={{ cornerSize: 20, rotatingPointOffset: 70, }} usageStatistics={true} ref={imageEditor} /> </div> )} </ImageContext.Consumer> ); }
But when I run the app, I encounter this error: TypeError: Cannot read property 'path' of undefined
Also react dev tools highlights the: path: value.path. as the mistake.
Do you think why?
All-Star
58144 Points
15646 Posts
Re: What is my mistake in using Context in React?
Feb 25, 2021 03:57 PM|bruce (sqlwork.com)|LINK
In the provider you set value to a function. So when you use value in the consumer it’s still a function. Normally you would call it in the consumer somewhere. as it a state setter you’d expect it to be used as an event handler. Not sure why you expect the function to have properties.
You should go thru some tutorials. You appear to be just trying random code samples.
Member
21 Points
246 Posts
Re: What is my mistake in using Context in React?
Feb 25, 2021 04:30 PM|SaeedP|LINK
I understood what you say but I read tutorials!