Can't expose react state from class









up vote
-1
down vote

favorite












I'm not able to expose the internal state of a class to an external function. It only returns the initial state of the class, although I made sure that the class state is updated, and the event is bound to the class instance.



//Class A
export class A extends React.Component<,t: string>
constructor(props)
super(props);
this.state =
t: "initialState"
;
this.parse = this.parse.bind(this);


public get_t(): string
return this.state.t;


parse(evt)
this.setState(
t: evt.target.value
, () =>
console.info("Text: " + this.state.t); //Prints the right value
);


render()
return (
<React.Fragment>
<input
type="text"
onChange=this.parse
/>
</React.Fragment>
);



// separate funciton
const run = (aList) =>
aList.map(item =>
console.info("T: " + item.item.get_t()); // This prints "initialState"
);



EDIT: the run function is the entry point (executed from main).



Can you help me out?










share|improve this question























  • You need to pass it as prop
    – Shireesha Parampalli
    Nov 11 at 10:59










  • Can you explain a bit more, I don't see why I should pass it as a prop. I'd prefer to have "t" getting fully managed by class A as it is very distinct from function "run".
    – Poka Yoke
    Nov 11 at 11:06










  • Please, provide stackoverflow.com/help/mcve that reflects your case . run isn't used anywhere.
    – estus
    Nov 11 at 13:22










  • I don't think it's relevant to the question, but I added it anyway. It's the function that invokes get_t() of the instance..
    – Poka Yoke
    Nov 11 at 13:27










  • A component state is private, and you should not rely on it outside of the component itself for many reasons. The right way to monitor the state would be to pass a callback as props that will be called for indicating the state changed (like dispatching the update to store or holding the values in the parent component, or any other solution that don't involve accessing the components state externally). please go over the state section here: reactjs.org/docs/state-and-lifecycle.html
    – Nirit Levi
    Nov 11 at 13:59















up vote
-1
down vote

favorite












I'm not able to expose the internal state of a class to an external function. It only returns the initial state of the class, although I made sure that the class state is updated, and the event is bound to the class instance.



//Class A
export class A extends React.Component<,t: string>
constructor(props)
super(props);
this.state =
t: "initialState"
;
this.parse = this.parse.bind(this);


public get_t(): string
return this.state.t;


parse(evt)
this.setState(
t: evt.target.value
, () =>
console.info("Text: " + this.state.t); //Prints the right value
);


render()
return (
<React.Fragment>
<input
type="text"
onChange=this.parse
/>
</React.Fragment>
);



// separate funciton
const run = (aList) =>
aList.map(item =>
console.info("T: " + item.item.get_t()); // This prints "initialState"
);



EDIT: the run function is the entry point (executed from main).



Can you help me out?










share|improve this question























  • You need to pass it as prop
    – Shireesha Parampalli
    Nov 11 at 10:59










  • Can you explain a bit more, I don't see why I should pass it as a prop. I'd prefer to have "t" getting fully managed by class A as it is very distinct from function "run".
    – Poka Yoke
    Nov 11 at 11:06










  • Please, provide stackoverflow.com/help/mcve that reflects your case . run isn't used anywhere.
    – estus
    Nov 11 at 13:22










  • I don't think it's relevant to the question, but I added it anyway. It's the function that invokes get_t() of the instance..
    – Poka Yoke
    Nov 11 at 13:27










  • A component state is private, and you should not rely on it outside of the component itself for many reasons. The right way to monitor the state would be to pass a callback as props that will be called for indicating the state changed (like dispatching the update to store or holding the values in the parent component, or any other solution that don't involve accessing the components state externally). please go over the state section here: reactjs.org/docs/state-and-lifecycle.html
    – Nirit Levi
    Nov 11 at 13:59













up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











I'm not able to expose the internal state of a class to an external function. It only returns the initial state of the class, although I made sure that the class state is updated, and the event is bound to the class instance.



//Class A
export class A extends React.Component<,t: string>
constructor(props)
super(props);
this.state =
t: "initialState"
;
this.parse = this.parse.bind(this);


public get_t(): string
return this.state.t;


parse(evt)
this.setState(
t: evt.target.value
, () =>
console.info("Text: " + this.state.t); //Prints the right value
);


render()
return (
<React.Fragment>
<input
type="text"
onChange=this.parse
/>
</React.Fragment>
);



// separate funciton
const run = (aList) =>
aList.map(item =>
console.info("T: " + item.item.get_t()); // This prints "initialState"
);



EDIT: the run function is the entry point (executed from main).



Can you help me out?










share|improve this question















I'm not able to expose the internal state of a class to an external function. It only returns the initial state of the class, although I made sure that the class state is updated, and the event is bound to the class instance.



//Class A
export class A extends React.Component<,t: string>
constructor(props)
super(props);
this.state =
t: "initialState"
;
this.parse = this.parse.bind(this);


public get_t(): string
return this.state.t;


parse(evt)
this.setState(
t: evt.target.value
, () =>
console.info("Text: " + this.state.t); //Prints the right value
);


render()
return (
<React.Fragment>
<input
type="text"
onChange=this.parse
/>
</React.Fragment>
);



// separate funciton
const run = (aList) =>
aList.map(item =>
console.info("T: " + item.item.get_t()); // This prints "initialState"
);



EDIT: the run function is the entry point (executed from main).



Can you help me out?







reactjs typescript






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 13:25

























asked Nov 11 at 10:58









Poka Yoke

1451725




1451725











  • You need to pass it as prop
    – Shireesha Parampalli
    Nov 11 at 10:59










  • Can you explain a bit more, I don't see why I should pass it as a prop. I'd prefer to have "t" getting fully managed by class A as it is very distinct from function "run".
    – Poka Yoke
    Nov 11 at 11:06










  • Please, provide stackoverflow.com/help/mcve that reflects your case . run isn't used anywhere.
    – estus
    Nov 11 at 13:22










  • I don't think it's relevant to the question, but I added it anyway. It's the function that invokes get_t() of the instance..
    – Poka Yoke
    Nov 11 at 13:27










  • A component state is private, and you should not rely on it outside of the component itself for many reasons. The right way to monitor the state would be to pass a callback as props that will be called for indicating the state changed (like dispatching the update to store or holding the values in the parent component, or any other solution that don't involve accessing the components state externally). please go over the state section here: reactjs.org/docs/state-and-lifecycle.html
    – Nirit Levi
    Nov 11 at 13:59

















  • You need to pass it as prop
    – Shireesha Parampalli
    Nov 11 at 10:59










  • Can you explain a bit more, I don't see why I should pass it as a prop. I'd prefer to have "t" getting fully managed by class A as it is very distinct from function "run".
    – Poka Yoke
    Nov 11 at 11:06










  • Please, provide stackoverflow.com/help/mcve that reflects your case . run isn't used anywhere.
    – estus
    Nov 11 at 13:22










  • I don't think it's relevant to the question, but I added it anyway. It's the function that invokes get_t() of the instance..
    – Poka Yoke
    Nov 11 at 13:27










  • A component state is private, and you should not rely on it outside of the component itself for many reasons. The right way to monitor the state would be to pass a callback as props that will be called for indicating the state changed (like dispatching the update to store or holding the values in the parent component, or any other solution that don't involve accessing the components state externally). please go over the state section here: reactjs.org/docs/state-and-lifecycle.html
    – Nirit Levi
    Nov 11 at 13:59
















You need to pass it as prop
– Shireesha Parampalli
Nov 11 at 10:59




You need to pass it as prop
– Shireesha Parampalli
Nov 11 at 10:59












Can you explain a bit more, I don't see why I should pass it as a prop. I'd prefer to have "t" getting fully managed by class A as it is very distinct from function "run".
– Poka Yoke
Nov 11 at 11:06




Can you explain a bit more, I don't see why I should pass it as a prop. I'd prefer to have "t" getting fully managed by class A as it is very distinct from function "run".
– Poka Yoke
Nov 11 at 11:06












Please, provide stackoverflow.com/help/mcve that reflects your case . run isn't used anywhere.
– estus
Nov 11 at 13:22




Please, provide stackoverflow.com/help/mcve that reflects your case . run isn't used anywhere.
– estus
Nov 11 at 13:22












I don't think it's relevant to the question, but I added it anyway. It's the function that invokes get_t() of the instance..
– Poka Yoke
Nov 11 at 13:27




I don't think it's relevant to the question, but I added it anyway. It's the function that invokes get_t() of the instance..
– Poka Yoke
Nov 11 at 13:27












A component state is private, and you should not rely on it outside of the component itself for many reasons. The right way to monitor the state would be to pass a callback as props that will be called for indicating the state changed (like dispatching the update to store or holding the values in the parent component, or any other solution that don't involve accessing the components state externally). please go over the state section here: reactjs.org/docs/state-and-lifecycle.html
– Nirit Levi
Nov 11 at 13:59





A component state is private, and you should not rely on it outside of the component itself for many reasons. The right way to monitor the state would be to pass a callback as props that will be called for indicating the state changed (like dispatching the update to store or holding the values in the parent component, or any other solution that don't involve accessing the components state externally). please go over the state section here: reactjs.org/docs/state-and-lifecycle.html
– Nirit Levi
Nov 11 at 13:59


















active

oldest

votes











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',
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%2f53248039%2fcant-expose-react-state-from-class%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes















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%2f53248039%2fcant-expose-react-state-from-class%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







這個網誌中的熱門文章

What does pagestruct do in Eviews?

Dutch intervention in Lombok and Karangasem

Channel Islands