How do I manage my array of children components' states?2019 Community Moderator ElectionIs 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
How to deal with taxi scam when on vacation?
What is a ^ b and (a & b) << 1?
How should I state my peer review experience in the CV?
Is a party consisting of only a bard, a cleric, and a warlock functional long-term?
Aluminum electrolytic or ceramic capacitors for linear regulator input and output?
Why do tuner card drivers fail to build after kernel update to 4.4.0-143-generic?
How to terminate ping <dest> &
Python if-else code style for reduced code for rounding floats
Recruiter wants very extensive technical details about all of my previous work
Tikz picture of two mathematical functions
What is the adequate fee for a reveal operation?
Official degrees of earth’s rotation per day
If I can solve Sudoku, can I solve the Travelling Salesman Problem (TSP)? If so, how?
What are substitutions for coconut in curry?
What options are left, if Britain cannot decide?
Book about superhumans hiding among normal humans
Is it good practice to use Linear Least-Squares with SMA?
Planetary tidal locking causing asymetrical water distribution
Unable to evaluate Eigenvalues and Eigenvectors for a matrix (2)
Print a physical multiplication table
Why did it take so long to abandon sail after steamships were demonstrated?
Employee lack of ownership
Are all passive ability checks floors for active ability checks?
Why one should not leave fingerprints on bulbs and plugs?
How do I manage my array of children components' states?
2019 Community Moderator ElectionIs 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
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.
reactjs firebase react-native firebase-storage
add a comment |
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.
reactjs firebase react-native firebase-storage
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, eitherconstructor
orParentComponent
, would be a solution. I'm not sure how to go about it: user clicksUploadButton
, 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
add a comment |
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.
reactjs firebase react-native firebase-storage
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
reactjs firebase react-native firebase-storage
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, eitherconstructor
orParentComponent
, would be a solution. I'm not sure how to go about it: user clicksUploadButton
, 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
add a comment |
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, eitherconstructor
orParentComponent
, would be a solution. I'm not sure how to go about it: user clicksUploadButton
, 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
add a comment |
1 Answer
1
active
oldest
votes
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>
);
YES! This was exactly the answer I was looking for! Thank you so much!
– bs.gk
Mar 11 at 18:55
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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>
);
YES! This was exactly the answer I was looking for! Thank you so much!
– bs.gk
Mar 11 at 18:55
add a comment |
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>
);
YES! This was exactly the answer I was looking for! Thank you so much!
– bs.gk
Mar 11 at 18:55
add a comment |
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>
);
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>
);
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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
orParentComponent
, would be a solution. I'm not sure how to go about it: user clicksUploadButton
, 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