React Native - Update Parent State in Child with Dynamic Key












0














I am very new to both Javascript and React Native, and I am trying update a parent's state by using a callback function using a dynamic key to avoid writing multiple functions.



In the parent component, I pass this function to the child to child to update the parent's state based on user text input. This code achieves the desired result.



In Parent:



_setAge = (value) => {
this.setState({age: value})}

<ChildComponent name = 'Update Age' _setAge = { this._setAge.bind(this) } />


In Child:



//Other child code
<TextInput onChangeText = { (input) => {this.props._setAge(input)} }
//Etc.


However, I am looking for a way to pass a desired state key from the parent to the child to update the state dynamically. I have tried the following:



In Parent:



const ageKey = 'age'

_setAge = (value, stateKey) => {
this.setState({ [stateKey]: value })}

<ChildComponent name = 'Update Age' _setAge = { this._setAge.bind(this) } stateKey = ageKey } />


In Child:



//Other child code
<TextInput onChangeText = { (input) => this.props._setAge(input, this.props.stateKey)
//Etc.


However this doesn't work. My current work around is writing 6 functions for my 6 child components, each updating the desire state. However, while this would work for my basic app, I am looking for a way that is more scalable for future projects. Thank you!










share|improve this question






















  • I put these snipits into a sandbox pretty much as is and it seems to work, maybe I'm misunderstanding your problem? codesandbox.io/s/95v45pj7w
    – SpeedOfRound
    Nov 21 '18 at 17:59












  • Hi SpeedOfRound! Thank you, it's working now. I probably was resolved an issue I did not know about while comparing your code to mine. I appreciate the time
    – holdenmaudlin
    Nov 21 '18 at 19:40
















0














I am very new to both Javascript and React Native, and I am trying update a parent's state by using a callback function using a dynamic key to avoid writing multiple functions.



In the parent component, I pass this function to the child to child to update the parent's state based on user text input. This code achieves the desired result.



In Parent:



_setAge = (value) => {
this.setState({age: value})}

<ChildComponent name = 'Update Age' _setAge = { this._setAge.bind(this) } />


In Child:



//Other child code
<TextInput onChangeText = { (input) => {this.props._setAge(input)} }
//Etc.


However, I am looking for a way to pass a desired state key from the parent to the child to update the state dynamically. I have tried the following:



In Parent:



const ageKey = 'age'

_setAge = (value, stateKey) => {
this.setState({ [stateKey]: value })}

<ChildComponent name = 'Update Age' _setAge = { this._setAge.bind(this) } stateKey = ageKey } />


In Child:



//Other child code
<TextInput onChangeText = { (input) => this.props._setAge(input, this.props.stateKey)
//Etc.


However this doesn't work. My current work around is writing 6 functions for my 6 child components, each updating the desire state. However, while this would work for my basic app, I am looking for a way that is more scalable for future projects. Thank you!










share|improve this question






















  • I put these snipits into a sandbox pretty much as is and it seems to work, maybe I'm misunderstanding your problem? codesandbox.io/s/95v45pj7w
    – SpeedOfRound
    Nov 21 '18 at 17:59












  • Hi SpeedOfRound! Thank you, it's working now. I probably was resolved an issue I did not know about while comparing your code to mine. I appreciate the time
    – holdenmaudlin
    Nov 21 '18 at 19:40














0












0








0







I am very new to both Javascript and React Native, and I am trying update a parent's state by using a callback function using a dynamic key to avoid writing multiple functions.



In the parent component, I pass this function to the child to child to update the parent's state based on user text input. This code achieves the desired result.



In Parent:



_setAge = (value) => {
this.setState({age: value})}

<ChildComponent name = 'Update Age' _setAge = { this._setAge.bind(this) } />


In Child:



//Other child code
<TextInput onChangeText = { (input) => {this.props._setAge(input)} }
//Etc.


However, I am looking for a way to pass a desired state key from the parent to the child to update the state dynamically. I have tried the following:



In Parent:



const ageKey = 'age'

_setAge = (value, stateKey) => {
this.setState({ [stateKey]: value })}

<ChildComponent name = 'Update Age' _setAge = { this._setAge.bind(this) } stateKey = ageKey } />


In Child:



//Other child code
<TextInput onChangeText = { (input) => this.props._setAge(input, this.props.stateKey)
//Etc.


However this doesn't work. My current work around is writing 6 functions for my 6 child components, each updating the desire state. However, while this would work for my basic app, I am looking for a way that is more scalable for future projects. Thank you!










share|improve this question













I am very new to both Javascript and React Native, and I am trying update a parent's state by using a callback function using a dynamic key to avoid writing multiple functions.



In the parent component, I pass this function to the child to child to update the parent's state based on user text input. This code achieves the desired result.



In Parent:



_setAge = (value) => {
this.setState({age: value})}

<ChildComponent name = 'Update Age' _setAge = { this._setAge.bind(this) } />


In Child:



//Other child code
<TextInput onChangeText = { (input) => {this.props._setAge(input)} }
//Etc.


However, I am looking for a way to pass a desired state key from the parent to the child to update the state dynamically. I have tried the following:



In Parent:



const ageKey = 'age'

_setAge = (value, stateKey) => {
this.setState({ [stateKey]: value })}

<ChildComponent name = 'Update Age' _setAge = { this._setAge.bind(this) } stateKey = ageKey } />


In Child:



//Other child code
<TextInput onChangeText = { (input) => this.props._setAge(input, this.props.stateKey)
//Etc.


However this doesn't work. My current work around is writing 6 functions for my 6 child components, each updating the desire state. However, while this would work for my basic app, I am looking for a way that is more scalable for future projects. Thank you!







javascript react-native






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 '18 at 17:31









holdenmaudlin

11




11












  • I put these snipits into a sandbox pretty much as is and it seems to work, maybe I'm misunderstanding your problem? codesandbox.io/s/95v45pj7w
    – SpeedOfRound
    Nov 21 '18 at 17:59












  • Hi SpeedOfRound! Thank you, it's working now. I probably was resolved an issue I did not know about while comparing your code to mine. I appreciate the time
    – holdenmaudlin
    Nov 21 '18 at 19:40


















  • I put these snipits into a sandbox pretty much as is and it seems to work, maybe I'm misunderstanding your problem? codesandbox.io/s/95v45pj7w
    – SpeedOfRound
    Nov 21 '18 at 17:59












  • Hi SpeedOfRound! Thank you, it's working now. I probably was resolved an issue I did not know about while comparing your code to mine. I appreciate the time
    – holdenmaudlin
    Nov 21 '18 at 19:40
















I put these snipits into a sandbox pretty much as is and it seems to work, maybe I'm misunderstanding your problem? codesandbox.io/s/95v45pj7w
– SpeedOfRound
Nov 21 '18 at 17:59






I put these snipits into a sandbox pretty much as is and it seems to work, maybe I'm misunderstanding your problem? codesandbox.io/s/95v45pj7w
– SpeedOfRound
Nov 21 '18 at 17:59














Hi SpeedOfRound! Thank you, it's working now. I probably was resolved an issue I did not know about while comparing your code to mine. I appreciate the time
– holdenmaudlin
Nov 21 '18 at 19:40




Hi SpeedOfRound! Thank you, it's working now. I probably was resolved an issue I did not know about while comparing your code to mine. I appreciate the time
– holdenmaudlin
Nov 21 '18 at 19:40












1 Answer
1






active

oldest

votes


















0
















In Parent
Instead of passing stateKey in props on child key directly pass the state key in onChageText method in child. the code would look like this->>>>





_setAge = (value, stateKey) => {  
this.setState({ [stateKey]: value })}

<ChildComponent name = 'Update Age' _setAge = {this._setAge} /> // providing no statekey here




Inside the child





<TextInput onChangeText = { (input) => this.props._setAge(input, 'age') } 
// here i know statekey for each child so i directly pass the key from child so i dont have to pass it as a props and then access it





share|improve this answer





















  • Hi md.warish Ansari thanks for the response. Unfortunately I am trying to avoid hard coding the key in the child component because I am re-using it 6 times. I should have been more clear with my variable names. I would like to pass in the key from the parent so each time I use the child component, I can customize which state of the parent it updates.
    – holdenmaudlin
    Nov 21 '18 at 18:46










  • @holdenmaudlin I recommend putting your "magic number" into a constants file and importing it as needed
    – SpeedOfRound
    Nov 21 '18 at 21:08











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%2f53417652%2freact-native-update-parent-state-in-child-with-dynamic-key%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
















In Parent
Instead of passing stateKey in props on child key directly pass the state key in onChageText method in child. the code would look like this->>>>





_setAge = (value, stateKey) => {  
this.setState({ [stateKey]: value })}

<ChildComponent name = 'Update Age' _setAge = {this._setAge} /> // providing no statekey here




Inside the child





<TextInput onChangeText = { (input) => this.props._setAge(input, 'age') } 
// here i know statekey for each child so i directly pass the key from child so i dont have to pass it as a props and then access it





share|improve this answer





















  • Hi md.warish Ansari thanks for the response. Unfortunately I am trying to avoid hard coding the key in the child component because I am re-using it 6 times. I should have been more clear with my variable names. I would like to pass in the key from the parent so each time I use the child component, I can customize which state of the parent it updates.
    – holdenmaudlin
    Nov 21 '18 at 18:46










  • @holdenmaudlin I recommend putting your "magic number" into a constants file and importing it as needed
    – SpeedOfRound
    Nov 21 '18 at 21:08
















0
















In Parent
Instead of passing stateKey in props on child key directly pass the state key in onChageText method in child. the code would look like this->>>>





_setAge = (value, stateKey) => {  
this.setState({ [stateKey]: value })}

<ChildComponent name = 'Update Age' _setAge = {this._setAge} /> // providing no statekey here




Inside the child





<TextInput onChangeText = { (input) => this.props._setAge(input, 'age') } 
// here i know statekey for each child so i directly pass the key from child so i dont have to pass it as a props and then access it





share|improve this answer





















  • Hi md.warish Ansari thanks for the response. Unfortunately I am trying to avoid hard coding the key in the child component because I am re-using it 6 times. I should have been more clear with my variable names. I would like to pass in the key from the parent so each time I use the child component, I can customize which state of the parent it updates.
    – holdenmaudlin
    Nov 21 '18 at 18:46










  • @holdenmaudlin I recommend putting your "magic number" into a constants file and importing it as needed
    – SpeedOfRound
    Nov 21 '18 at 21:08














0












0








0








In Parent
Instead of passing stateKey in props on child key directly pass the state key in onChageText method in child. the code would look like this->>>>





_setAge = (value, stateKey) => {  
this.setState({ [stateKey]: value })}

<ChildComponent name = 'Update Age' _setAge = {this._setAge} /> // providing no statekey here




Inside the child





<TextInput onChangeText = { (input) => this.props._setAge(input, 'age') } 
// here i know statekey for each child so i directly pass the key from child so i dont have to pass it as a props and then access it





share|improve this answer














In Parent
Instead of passing stateKey in props on child key directly pass the state key in onChageText method in child. the code would look like this->>>>





_setAge = (value, stateKey) => {  
this.setState({ [stateKey]: value })}

<ChildComponent name = 'Update Age' _setAge = {this._setAge} /> // providing no statekey here




Inside the child





<TextInput onChangeText = { (input) => this.props._setAge(input, 'age') } 
// here i know statekey for each child so i directly pass the key from child so i dont have to pass it as a props and then access it






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 '18 at 17:55









md.warish Ansari

772




772












  • Hi md.warish Ansari thanks for the response. Unfortunately I am trying to avoid hard coding the key in the child component because I am re-using it 6 times. I should have been more clear with my variable names. I would like to pass in the key from the parent so each time I use the child component, I can customize which state of the parent it updates.
    – holdenmaudlin
    Nov 21 '18 at 18:46










  • @holdenmaudlin I recommend putting your "magic number" into a constants file and importing it as needed
    – SpeedOfRound
    Nov 21 '18 at 21:08


















  • Hi md.warish Ansari thanks for the response. Unfortunately I am trying to avoid hard coding the key in the child component because I am re-using it 6 times. I should have been more clear with my variable names. I would like to pass in the key from the parent so each time I use the child component, I can customize which state of the parent it updates.
    – holdenmaudlin
    Nov 21 '18 at 18:46










  • @holdenmaudlin I recommend putting your "magic number" into a constants file and importing it as needed
    – SpeedOfRound
    Nov 21 '18 at 21:08
















Hi md.warish Ansari thanks for the response. Unfortunately I am trying to avoid hard coding the key in the child component because I am re-using it 6 times. I should have been more clear with my variable names. I would like to pass in the key from the parent so each time I use the child component, I can customize which state of the parent it updates.
– holdenmaudlin
Nov 21 '18 at 18:46




Hi md.warish Ansari thanks for the response. Unfortunately I am trying to avoid hard coding the key in the child component because I am re-using it 6 times. I should have been more clear with my variable names. I would like to pass in the key from the parent so each time I use the child component, I can customize which state of the parent it updates.
– holdenmaudlin
Nov 21 '18 at 18:46












@holdenmaudlin I recommend putting your "magic number" into a constants file and importing it as needed
– SpeedOfRound
Nov 21 '18 at 21:08




@holdenmaudlin I recommend putting your "magic number" into a constants file and importing it as needed
– SpeedOfRound
Nov 21 '18 at 21:08


















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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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%2f53417652%2freact-native-update-parent-state-in-child-with-dynamic-key%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

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'