How do I manage my array of children components' states? The Next CEO of Stack OverflowIs it a good practice to call setState or other methods on this.props.children in React?Correct modification of state arrays in ReactJSUnderstanding unique keys for array children in React.jsCan you force a React component to rerender without calling setState?How to update parent's state in React?react-konva How to change image after uploading?`setState` in React portal containing a text input causes browser to scroll in SafariNot able to navigate to other page in react nativeHow to correctly handle data object from REST API using fetch in Reactnavigate from another screen on click navigationDrawerLayout menu

Is there a difference between "Fahrstuhl" and "Aufzug"

Is "for causing autism in X" grammatical?

If/When UK leaves the EU, can a future goverment conduct a referendum to join the EU?

Why do professional authors make "consistency" mistakes? And how to avoid them?

Received an invoice from my ex-employer billing me for training; how to handle?

Is it possible to search for a directory/file combination?

Is HostGator storing my password in plaintext?

How do I go from 300 unfinished/half written blog posts, to published posts?

Example of a Mathematician/Physicist whose Other Publications during their PhD eclipsed their PhD Thesis

Return the Closest Prime Number

Is it ever safe to open a suspicious html file (e.g. email attachment)?

Why does the UK parliament need a vote on the political declaration?

Which tube will fit a -(700 x 25c) wheel?

What happened in Rome, when the western empire "fell"?

How fast would a person need to move to trick the eye?

Parametric curve length - calculus

Are there any limitations on attacking while grappling?

Rotate a column

Unreliable Magic - Is it worth it?

MessageLevel in QGIS3

How do we know the LHC results are robust?

Several mode to write the symbol of a vector

Can I equip Skullclamp on a creature I am sacrificing?

Is it my responsibility to learn a new technology in my own time my employer wants to implement?



How do I manage my array of children components' states?



The Next CEO of Stack OverflowIs it a good practice to call setState or other methods on this.props.children in React?Correct modification of state arrays in ReactJSUnderstanding unique keys for array children in React.jsCan you force a React component to rerender without calling setState?How to update parent's state in React?react-konva How to change image after uploading?`setState` in React portal containing a text input causes browser to scroll in SafariNot able to navigate to other page in react nativeHow to correctly handle data object from REST API using fetch in Reactnavigate from another screen on click navigationDrawerLayout menu










2















I'm new to react, so forgive me. I'm having a problem understanding states, specifically those of children.



Purpose: I'm trying to create a form that a user can append more and more components -- in this case, images.



What happens: User appends 2 or more images. User tries to upload an image with UploadButton component, but both the images are the same. I believe this has to do with both appended children sharing the same state.



Question: How do I give each appended child its own image without affecting the other appended children?



class Page extends Component
constructor (props)
super(props);

this.state =
id: '',
numChildren: 0,
images: [],

this.onAddChild = this.onAddChild.bind(this);


showModal()
this.setState(
numChildren: 0,
images: [],
);


renderModal()
const children = [];
//Here's my array of child components
for(var i = 0; i < this.state.numChildren; i += 1)
children.push(<this.ChildComponent key=i />);


return (
<ReactModal>
<this.ParentComponent addChild=this.onAddChild>
children
</this.ParentComponent>
</ReactModal>
)
}

onAddChild = () =>
this.setState(
numChildren: this.state.numChildren + 1
)


ParentComponent = (props) => (
<div>
props.children
<Button onClick=props.addChild>Add Item</Button>
</div>
);

ChildComponent = () => (
<div>
<UploadButton
storage="menus"
value=this.state.images
onUploadComplete=uri => this.setState(images: uri)
/>
</div>
);
}


Here's the code for UploadButton:



import React, Component from 'react';
import uuid from 'uuid';
import firebase from '../config/firebase';

class UploadButton extends Component

constructor(props)
super(props);

this.state =
isUploading: false



handleClick()
const input = document.createElement("INPUT");
input.setAttribute("type", "file");
input.setAttribute("accept", "image/gif, image/jpeg, image/png");
input.addEventListener("change", (target: files: [file]) => this.uploadFile(file));
input.click();


uploadFile(file)
console.log('F', file);
const id = uuid.v4();
this.setState( isUploading: true )
const metadata =
contentType: file.type
;
firebase.storage()
.ref('friends')
.child(id)
.put(file, metadata)
.then(( downloadURL ) =>
this.setState( isUploading: false )
console.log('Uploaded', downloadURL);
this.props.onUploadComplete(downloadURL);
)
.catch(e => this.setState( isUploading: false ));



render()
const
props:
value,
style = ,
className = "image-upload-button",
,
state:
isUploading

= this;

return (
<div
onClick=() => this.handleClick()
className=className
style=
...style,
backgroundImage: `url("$this.props.value")`,
>
isUploading ? "UPLOADING..." : !value ? 'No image' : ''
</div>
);





export default UploadButton;


I tried to exclude all unnecessary code not pertaining to my problem, but please, let me know if I need to show more.



EDIT: This is my attempt, it doesn't work:






//altered my children array to include a new prop
renderModal()
const children = [];
for (var i = 0; i < this.state.numChildren; i += 1)
children.push(<this.ChildComponent imageSelect=this.onImageSelect key=i />);

//...
;

//my attempt to assign value and pass selected image back to images array
ChildComponent = () => (
<div>
<UploadButton
storage="menus"
value=uri => this.props.onImageSelect(uri) //my greenness is really apparent here
onUploadComplete=uri => this.setState(images: uri)
/>
//...
</div>
);

//added this function to the class
onImageSelect(uri)
var el = this.state.images.concat(uri);
this.setState(
images: el
)





I know I'm not accessing the child prop correctly. This is the most complexity I've dealt with so far. Thanks for your time.










share|improve this question
























  • TLDR Have you considered keeping all images in the parent in an array and iterating through it to create the children.

    – Miro J.
    Mar 7 at 16:03











  • @Miro J. I've thought that an array of images somewhere, either constructor or ParentComponent, would be a solution. I'm not sure how to go about it: user clicks UploadButton, the selected image gets put into an image array, then iterating through the array creates all the children. Currently, the user appends the children before selecting the images. I heard somewhere that programmers spend 90% of their time, not typing, but thinking of how to implement their ideas while staring at the computer screen and I'm experiencing that for three days now.

    – bs.gk
    Mar 7 at 16:21











  • Google "how to call a parent function from a child component in react" > Create a function in the child for transferring the image to the parent after uploading finishes. The parent array should have a maximum of one "empty" element, which will show the upload functionality in the child.

    – Miro J.
    Mar 7 at 16:28











  • @Miro J. I'm trying to implement what you suggested now. I have a feeling part of the problem is because I'm using stateless components, so editing a lot of my code.

    – bs.gk
    Mar 7 at 17:18















2















I'm new to react, so forgive me. I'm having a problem understanding states, specifically those of children.



Purpose: I'm trying to create a form that a user can append more and more components -- in this case, images.



What happens: User appends 2 or more images. User tries to upload an image with UploadButton component, but both the images are the same. I believe this has to do with both appended children sharing the same state.



Question: How do I give each appended child its own image without affecting the other appended children?



class Page extends Component
constructor (props)
super(props);

this.state =
id: '',
numChildren: 0,
images: [],

this.onAddChild = this.onAddChild.bind(this);


showModal()
this.setState(
numChildren: 0,
images: [],
);


renderModal()
const children = [];
//Here's my array of child components
for(var i = 0; i < this.state.numChildren; i += 1)
children.push(<this.ChildComponent key=i />);


return (
<ReactModal>
<this.ParentComponent addChild=this.onAddChild>
children
</this.ParentComponent>
</ReactModal>
)
}

onAddChild = () =>
this.setState(
numChildren: this.state.numChildren + 1
)


ParentComponent = (props) => (
<div>
props.children
<Button onClick=props.addChild>Add Item</Button>
</div>
);

ChildComponent = () => (
<div>
<UploadButton
storage="menus"
value=this.state.images
onUploadComplete=uri => this.setState(images: uri)
/>
</div>
);
}


Here's the code for UploadButton:



import React, Component from 'react';
import uuid from 'uuid';
import firebase from '../config/firebase';

class UploadButton extends Component

constructor(props)
super(props);

this.state =
isUploading: false



handleClick()
const input = document.createElement("INPUT");
input.setAttribute("type", "file");
input.setAttribute("accept", "image/gif, image/jpeg, image/png");
input.addEventListener("change", (target: files: [file]) => this.uploadFile(file));
input.click();


uploadFile(file)
console.log('F', file);
const id = uuid.v4();
this.setState( isUploading: true )
const metadata =
contentType: file.type
;
firebase.storage()
.ref('friends')
.child(id)
.put(file, metadata)
.then(( downloadURL ) =>
this.setState( isUploading: false )
console.log('Uploaded', downloadURL);
this.props.onUploadComplete(downloadURL);
)
.catch(e => this.setState( isUploading: false ));



render()
const
props:
value,
style = ,
className = "image-upload-button",
,
state:
isUploading

= this;

return (
<div
onClick=() => this.handleClick()
className=className
style=
...style,
backgroundImage: `url("$this.props.value")`,
>
isUploading ? "UPLOADING..." : !value ? 'No image' : ''
</div>
);





export default UploadButton;


I tried to exclude all unnecessary code not pertaining to my problem, but please, let me know if I need to show more.



EDIT: This is my attempt, it doesn't work:






//altered my children array to include a new prop
renderModal()
const children = [];
for (var i = 0; i < this.state.numChildren; i += 1)
children.push(<this.ChildComponent imageSelect=this.onImageSelect key=i />);

//...
;

//my attempt to assign value and pass selected image back to images array
ChildComponent = () => (
<div>
<UploadButton
storage="menus"
value=uri => this.props.onImageSelect(uri) //my greenness is really apparent here
onUploadComplete=uri => this.setState(images: uri)
/>
//...
</div>
);

//added this function to the class
onImageSelect(uri)
var el = this.state.images.concat(uri);
this.setState(
images: el
)





I know I'm not accessing the child prop correctly. This is the most complexity I've dealt with so far. Thanks for your time.










share|improve this question
























  • TLDR Have you considered keeping all images in the parent in an array and iterating through it to create the children.

    – Miro J.
    Mar 7 at 16:03











  • @Miro J. I've thought that an array of images somewhere, either constructor or ParentComponent, would be a solution. I'm not sure how to go about it: user clicks UploadButton, the selected image gets put into an image array, then iterating through the array creates all the children. Currently, the user appends the children before selecting the images. I heard somewhere that programmers spend 90% of their time, not typing, but thinking of how to implement their ideas while staring at the computer screen and I'm experiencing that for three days now.

    – bs.gk
    Mar 7 at 16:21











  • Google "how to call a parent function from a child component in react" > Create a function in the child for transferring the image to the parent after uploading finishes. The parent array should have a maximum of one "empty" element, which will show the upload functionality in the child.

    – Miro J.
    Mar 7 at 16:28











  • @Miro J. I'm trying to implement what you suggested now. I have a feeling part of the problem is because I'm using stateless components, so editing a lot of my code.

    – bs.gk
    Mar 7 at 17:18













2












2








2








I'm new to react, so forgive me. I'm having a problem understanding states, specifically those of children.



Purpose: I'm trying to create a form that a user can append more and more components -- in this case, images.



What happens: User appends 2 or more images. User tries to upload an image with UploadButton component, but both the images are the same. I believe this has to do with both appended children sharing the same state.



Question: How do I give each appended child its own image without affecting the other appended children?



class Page extends Component
constructor (props)
super(props);

this.state =
id: '',
numChildren: 0,
images: [],

this.onAddChild = this.onAddChild.bind(this);


showModal()
this.setState(
numChildren: 0,
images: [],
);


renderModal()
const children = [];
//Here's my array of child components
for(var i = 0; i < this.state.numChildren; i += 1)
children.push(<this.ChildComponent key=i />);


return (
<ReactModal>
<this.ParentComponent addChild=this.onAddChild>
children
</this.ParentComponent>
</ReactModal>
)
}

onAddChild = () =>
this.setState(
numChildren: this.state.numChildren + 1
)


ParentComponent = (props) => (
<div>
props.children
<Button onClick=props.addChild>Add Item</Button>
</div>
);

ChildComponent = () => (
<div>
<UploadButton
storage="menus"
value=this.state.images
onUploadComplete=uri => this.setState(images: uri)
/>
</div>
);
}


Here's the code for UploadButton:



import React, Component from 'react';
import uuid from 'uuid';
import firebase from '../config/firebase';

class UploadButton extends Component

constructor(props)
super(props);

this.state =
isUploading: false



handleClick()
const input = document.createElement("INPUT");
input.setAttribute("type", "file");
input.setAttribute("accept", "image/gif, image/jpeg, image/png");
input.addEventListener("change", (target: files: [file]) => this.uploadFile(file));
input.click();


uploadFile(file)
console.log('F', file);
const id = uuid.v4();
this.setState( isUploading: true )
const metadata =
contentType: file.type
;
firebase.storage()
.ref('friends')
.child(id)
.put(file, metadata)
.then(( downloadURL ) =>
this.setState( isUploading: false )
console.log('Uploaded', downloadURL);
this.props.onUploadComplete(downloadURL);
)
.catch(e => this.setState( isUploading: false ));



render()
const
props:
value,
style = ,
className = "image-upload-button",
,
state:
isUploading

= this;

return (
<div
onClick=() => this.handleClick()
className=className
style=
...style,
backgroundImage: `url("$this.props.value")`,
>
isUploading ? "UPLOADING..." : !value ? 'No image' : ''
</div>
);





export default UploadButton;


I tried to exclude all unnecessary code not pertaining to my problem, but please, let me know if I need to show more.



EDIT: This is my attempt, it doesn't work:






//altered my children array to include a new prop
renderModal()
const children = [];
for (var i = 0; i < this.state.numChildren; i += 1)
children.push(<this.ChildComponent imageSelect=this.onImageSelect key=i />);

//...
;

//my attempt to assign value and pass selected image back to images array
ChildComponent = () => (
<div>
<UploadButton
storage="menus"
value=uri => this.props.onImageSelect(uri) //my greenness is really apparent here
onUploadComplete=uri => this.setState(images: uri)
/>
//...
</div>
);

//added this function to the class
onImageSelect(uri)
var el = this.state.images.concat(uri);
this.setState(
images: el
)





I know I'm not accessing the child prop correctly. This is the most complexity I've dealt with so far. Thanks for your time.










share|improve this question
















I'm new to react, so forgive me. I'm having a problem understanding states, specifically those of children.



Purpose: I'm trying to create a form that a user can append more and more components -- in this case, images.



What happens: User appends 2 or more images. User tries to upload an image with UploadButton component, but both the images are the same. I believe this has to do with both appended children sharing the same state.



Question: How do I give each appended child its own image without affecting the other appended children?



class Page extends Component
constructor (props)
super(props);

this.state =
id: '',
numChildren: 0,
images: [],

this.onAddChild = this.onAddChild.bind(this);


showModal()
this.setState(
numChildren: 0,
images: [],
);


renderModal()
const children = [];
//Here's my array of child components
for(var i = 0; i < this.state.numChildren; i += 1)
children.push(<this.ChildComponent key=i />);


return (
<ReactModal>
<this.ParentComponent addChild=this.onAddChild>
children
</this.ParentComponent>
</ReactModal>
)
}

onAddChild = () =>
this.setState(
numChildren: this.state.numChildren + 1
)


ParentComponent = (props) => (
<div>
props.children
<Button onClick=props.addChild>Add Item</Button>
</div>
);

ChildComponent = () => (
<div>
<UploadButton
storage="menus"
value=this.state.images
onUploadComplete=uri => this.setState(images: uri)
/>
</div>
);
}


Here's the code for UploadButton:



import React, Component from 'react';
import uuid from 'uuid';
import firebase from '../config/firebase';

class UploadButton extends Component

constructor(props)
super(props);

this.state =
isUploading: false



handleClick()
const input = document.createElement("INPUT");
input.setAttribute("type", "file");
input.setAttribute("accept", "image/gif, image/jpeg, image/png");
input.addEventListener("change", (target: files: [file]) => this.uploadFile(file));
input.click();


uploadFile(file)
console.log('F', file);
const id = uuid.v4();
this.setState( isUploading: true )
const metadata =
contentType: file.type
;
firebase.storage()
.ref('friends')
.child(id)
.put(file, metadata)
.then(( downloadURL ) =>
this.setState( isUploading: false )
console.log('Uploaded', downloadURL);
this.props.onUploadComplete(downloadURL);
)
.catch(e => this.setState( isUploading: false ));



render()
const
props:
value,
style = ,
className = "image-upload-button",
,
state:
isUploading

= this;

return (
<div
onClick=() => this.handleClick()
className=className
style=
...style,
backgroundImage: `url("$this.props.value")`,
>
isUploading ? "UPLOADING..." : !value ? 'No image' : ''
</div>
);





export default UploadButton;


I tried to exclude all unnecessary code not pertaining to my problem, but please, let me know if I need to show more.



EDIT: This is my attempt, it doesn't work:






//altered my children array to include a new prop
renderModal()
const children = [];
for (var i = 0; i < this.state.numChildren; i += 1)
children.push(<this.ChildComponent imageSelect=this.onImageSelect key=i />);

//...
;

//my attempt to assign value and pass selected image back to images array
ChildComponent = () => (
<div>
<UploadButton
storage="menus"
value=uri => this.props.onImageSelect(uri) //my greenness is really apparent here
onUploadComplete=uri => this.setState(images: uri)
/>
//...
</div>
);

//added this function to the class
onImageSelect(uri)
var el = this.state.images.concat(uri);
this.setState(
images: el
)





I know I'm not accessing the child prop correctly. This is the most complexity I've dealt with so far. Thanks for your time.






//altered my children array to include a new prop
renderModal()
const children = [];
for (var i = 0; i < this.state.numChildren; i += 1)
children.push(<this.ChildComponent imageSelect=this.onImageSelect key=i />);

//...
;

//my attempt to assign value and pass selected image back to images array
ChildComponent = () => (
<div>
<UploadButton
storage="menus"
value=uri => this.props.onImageSelect(uri) //my greenness is really apparent here
onUploadComplete=uri => this.setState(images: uri)
/>
//...
</div>
);

//added this function to the class
onImageSelect(uri)
var el = this.state.images.concat(uri);
this.setState(
images: el
)





//altered my children array to include a new prop
renderModal()
const children = [];
for (var i = 0; i < this.state.numChildren; i += 1)
children.push(<this.ChildComponent imageSelect=this.onImageSelect key=i />);

//...
;

//my attempt to assign value and pass selected image back to images array
ChildComponent = () => (
<div>
<UploadButton
storage="menus"
value=uri => this.props.onImageSelect(uri) //my greenness is really apparent here
onUploadComplete=uri => this.setState(images: uri)
/>
//...
</div>
);

//added this function to the class
onImageSelect(uri)
var el = this.state.images.concat(uri);
this.setState(
images: el
)






reactjs firebase react-native firebase-storage






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 7 at 18:06







bs.gk

















asked Mar 7 at 15:28









bs.gkbs.gk

115




115












  • TLDR Have you considered keeping all images in the parent in an array and iterating through it to create the children.

    – Miro J.
    Mar 7 at 16:03











  • @Miro J. I've thought that an array of images somewhere, either constructor or ParentComponent, would be a solution. I'm not sure how to go about it: user clicks UploadButton, the selected image gets put into an image array, then iterating through the array creates all the children. Currently, the user appends the children before selecting the images. I heard somewhere that programmers spend 90% of their time, not typing, but thinking of how to implement their ideas while staring at the computer screen and I'm experiencing that for three days now.

    – bs.gk
    Mar 7 at 16:21











  • Google "how to call a parent function from a child component in react" > Create a function in the child for transferring the image to the parent after uploading finishes. The parent array should have a maximum of one "empty" element, which will show the upload functionality in the child.

    – Miro J.
    Mar 7 at 16:28











  • @Miro J. I'm trying to implement what you suggested now. I have a feeling part of the problem is because I'm using stateless components, so editing a lot of my code.

    – bs.gk
    Mar 7 at 17:18

















  • TLDR Have you considered keeping all images in the parent in an array and iterating through it to create the children.

    – Miro J.
    Mar 7 at 16:03











  • @Miro J. I've thought that an array of images somewhere, either constructor or ParentComponent, would be a solution. I'm not sure how to go about it: user clicks UploadButton, the selected image gets put into an image array, then iterating through the array creates all the children. Currently, the user appends the children before selecting the images. I heard somewhere that programmers spend 90% of their time, not typing, but thinking of how to implement their ideas while staring at the computer screen and I'm experiencing that for three days now.

    – bs.gk
    Mar 7 at 16:21











  • Google "how to call a parent function from a child component in react" > Create a function in the child for transferring the image to the parent after uploading finishes. The parent array should have a maximum of one "empty" element, which will show the upload functionality in the child.

    – Miro J.
    Mar 7 at 16:28











  • @Miro J. I'm trying to implement what you suggested now. I have a feeling part of the problem is because I'm using stateless components, so editing a lot of my code.

    – bs.gk
    Mar 7 at 17:18
















TLDR Have you considered keeping all images in the parent in an array and iterating through it to create the children.

– Miro J.
Mar 7 at 16:03





TLDR Have you considered keeping all images in the parent in an array and iterating through it to create the children.

– Miro J.
Mar 7 at 16:03













@Miro J. I've thought that an array of images somewhere, either constructor or ParentComponent, would be a solution. I'm not sure how to go about it: user clicks UploadButton, the selected image gets put into an image array, then iterating through the array creates all the children. Currently, the user appends the children before selecting the images. I heard somewhere that programmers spend 90% of their time, not typing, but thinking of how to implement their ideas while staring at the computer screen and I'm experiencing that for three days now.

– bs.gk
Mar 7 at 16:21





@Miro J. I've thought that an array of images somewhere, either constructor or ParentComponent, would be a solution. I'm not sure how to go about it: user clicks UploadButton, the selected image gets put into an image array, then iterating through the array creates all the children. Currently, the user appends the children before selecting the images. I heard somewhere that programmers spend 90% of their time, not typing, but thinking of how to implement their ideas while staring at the computer screen and I'm experiencing that for three days now.

– bs.gk
Mar 7 at 16:21













Google "how to call a parent function from a child component in react" > Create a function in the child for transferring the image to the parent after uploading finishes. The parent array should have a maximum of one "empty" element, which will show the upload functionality in the child.

– Miro J.
Mar 7 at 16:28





Google "how to call a parent function from a child component in react" > Create a function in the child for transferring the image to the parent after uploading finishes. The parent array should have a maximum of one "empty" element, which will show the upload functionality in the child.

– Miro J.
Mar 7 at 16:28













@Miro J. I'm trying to implement what you suggested now. I have a feeling part of the problem is because I'm using stateless components, so editing a lot of my code.

– bs.gk
Mar 7 at 17:18





@Miro J. I'm trying to implement what you suggested now. I have a feeling part of the problem is because I'm using stateless components, so editing a lot of my code.

– bs.gk
Mar 7 at 17:18












1 Answer
1






active

oldest

votes


















0














When you write this.state in Child / Parent component, you are actually accessing the state of Page. Now, I would recommend that you pass in the index of the child to the Child like so



children.push(<this.ChildComponent key=i index=i/>)



so that each children deals with only its own image like so



ChildComponent = (index) => (
<div>
<UploadButton
storage="menus"
value=this.state.images[index]
onUploadComplete=uri =>
let images = this.state.images.slice()
images[index] = uri
this.setState(images)

/>
</div>
);





share|improve this answer























  • YES! This was exactly the answer I was looking for! Thank you so much!

    – bs.gk
    Mar 11 at 18:55











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55047367%2fhow-do-i-manage-my-array-of-children-components-states%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














When you write this.state in Child / Parent component, you are actually accessing the state of Page. Now, I would recommend that you pass in the index of the child to the Child like so



children.push(<this.ChildComponent key=i index=i/>)



so that each children deals with only its own image like so



ChildComponent = (index) => (
<div>
<UploadButton
storage="menus"
value=this.state.images[index]
onUploadComplete=uri =>
let images = this.state.images.slice()
images[index] = uri
this.setState(images)

/>
</div>
);





share|improve this answer























  • YES! This was exactly the answer I was looking for! Thank you so much!

    – bs.gk
    Mar 11 at 18:55















0














When you write this.state in Child / Parent component, you are actually accessing the state of Page. Now, I would recommend that you pass in the index of the child to the Child like so



children.push(<this.ChildComponent key=i index=i/>)



so that each children deals with only its own image like so



ChildComponent = (index) => (
<div>
<UploadButton
storage="menus"
value=this.state.images[index]
onUploadComplete=uri =>
let images = this.state.images.slice()
images[index] = uri
this.setState(images)

/>
</div>
);





share|improve this answer























  • YES! This was exactly the answer I was looking for! Thank you so much!

    – bs.gk
    Mar 11 at 18:55













0












0








0







When you write this.state in Child / Parent component, you are actually accessing the state of Page. Now, I would recommend that you pass in the index of the child to the Child like so



children.push(<this.ChildComponent key=i index=i/>)



so that each children deals with only its own image like so



ChildComponent = (index) => (
<div>
<UploadButton
storage="menus"
value=this.state.images[index]
onUploadComplete=uri =>
let images = this.state.images.slice()
images[index] = uri
this.setState(images)

/>
</div>
);





share|improve this answer













When you write this.state in Child / Parent component, you are actually accessing the state of Page. Now, I would recommend that you pass in the index of the child to the Child like so



children.push(<this.ChildComponent key=i index=i/>)



so that each children deals with only its own image like so



ChildComponent = (index) => (
<div>
<UploadButton
storage="menus"
value=this.state.images[index]
onUploadComplete=uri =>
let images = this.state.images.slice()
images[index] = uri
this.setState(images)

/>
</div>
);






share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 8 at 2:42









Murtaza RajaMurtaza Raja

10218




10218












  • YES! This was exactly the answer I was looking for! Thank you so much!

    – bs.gk
    Mar 11 at 18:55

















  • YES! This was exactly the answer I was looking for! Thank you so much!

    – bs.gk
    Mar 11 at 18:55
















YES! This was exactly the answer I was looking for! Thank you so much!

– bs.gk
Mar 11 at 18:55





YES! This was exactly the answer I was looking for! Thank you so much!

– bs.gk
Mar 11 at 18:55



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55047367%2fhow-do-i-manage-my-array-of-children-components-states%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

AWS Lex not identifying response if by a variable The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) The Ask Question Wizard is Live! Data science time! April 2019 and salary with experienceEnforcing custom enumeration in AWS LEX for slot valuesHow to give response based on user response in Amazon Lex?Intercepting AWS Lambda Response to a AWS Lex QueryLex chat bot error: Reached second execution of fulfillment lambda on the same utteranceamazon lex showing invalid responseLambda response send back to Lex slot?Response card in Amazon lexAmazon Lex - Lambda response return HTML to botHow can I solve 424 (Failed Dependency) (python) obtained from Amazon lex?

Алба-Юлія

Захаров Федір Захарович