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?
reactjs typescript
|
show 1 more comment
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?
reactjs typescript
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 .runisn'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
|
show 1 more comment
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?
reactjs typescript
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
reactjs typescript
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 .runisn'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
|
show 1 more comment
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 .runisn'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
|
show 1 more comment
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
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%2f53248039%2fcant-expose-react-state-from-class%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
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 .
runisn'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