0

I have the below Code: Where I have an interface type for a state. How do i initialize it in constructor method. As It doesn't allow me to initialize to string/ null/ number

interface IState { selectedUser?: IUserMenu}

class AssignUser extends React.Component<IProps, IState>{ constructor(props: IProps) { super(props); this.state = { selectedUser: ' what i have to put here' }}}

Also what does it mean by this.state ={} in the constructor function means ?

IUserMenu is an interface, so I cannot initialize to number, '', null,

Only thing I can use Is undefined

1
  • The this.state = {} call in the constructor sets the initial state for the component. Perhaps just pass this.state = {} if you don't have an initial value for state.selectedUser -- your interface indicates it is optional. Commented May 30, 2019 at 12:59

2 Answers 2

1

Hi @ankitjayaprakash ,

I've an example with your case where I'm initializing the object you want to pass described in the interface (but without being optional)

You can see it here

What "this.state={}" serves in the constructor is only to initialize the state of that component, even though it could be out without any builder function.

Sign up to request clarification or add additional context in comments.

3 Comments

Type 'null' is not assignable to type 'IUserMenu | undefined'. Am getting this error
It works for me without any error on the part of Typescript but if you want better open a CodeSandbox or a StackBlitz and copy all the code. This way we can help you better.
Thanks @fg93 for you effort, I works but I get TSLint suggestion. Thats fine. thanks
0

Your IState interface specifies that it has one property called selectedUser that is optional (as it has a ? after it).

If you do provide a selectedUser, it must of type IUserMenu. So your what i have to put here is an object that conforms to the IUserMenu interface.

this.state = {} means initialise the state with the object defined in the braces. In your case the object defined in the braces must match your IState interface (so contain a single property called selectedUser that is undefined of type IUserMenu)

5 Comments

Iam using selectedUser state in the Picker props selectedUser But I need to initialize this state in the constructor But my <item> value is an object In this case what should i have initialized to ?
What does the IUserMenu interface look like? And is one of these types passed in as a prop?
interface IUserMenu { name: string; surname: string; }
Ok, well it doesn't seem to make sense to me to initialise it to anything... you could just do: this.state = { selectedUser: undefined } or not bother setting the state at all in the constructor
Or, you could even intialise it like this: this.state = {selectedUser: { name: "", surname: "" }}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.