0

I'm new to the Typescript and I'm getting error when I'm trying to console.log input field value. Any suggestions? Code :

class Register extends Component<{},userState> {

state = {
    userList : []
}

emailEl= createRef<HTMLInputElement>()
passwordEl= createRef<HTMLInputElement>()
confirmpasswordEl= createRef<HTMLInputElement>()

registerUser = () => {
    const {userList} = this.state
    const destructList = [...userList]
    // const newUser = [destructList, {
    //     email:this.emailEl.current.value,
    //     password:this.passwordEl.current.value
    //
    // }]
    console.log('--------destructList', this.emailEl.current);

}



render() {
    return (
        <div className="container">
            <h1 className={'registerH1'}>Registration</h1>
            <div className="email">
                <input ref={this.emailEl}  type={'email'} placeholder={'Enter your E-mail'} />
            </div>
            <div ref={this.passwordEl} className="password">
                <input type={'password'} placeholder={'Enter your password'} />
            </div>
            <div ref={this.confirmpasswordEl} className="confirmPassword">
                <input type={'password'} placeholder={'Confirm your password'} />
            </div>
            <div id={'buttons'} className="buttons">
                <Link to={'/login'} >
                    <button>Log In</button>
                </Link>
                <button  onClick={() => this.registerUser()}>Registration</button>
            </div>
        </div>
    )
}

and here is the error :

TS2531: Object is possibly 'null'
5
  • 1
    Would you mind telling what the error message says? Commented Jun 25, 2020 at 14:24
  • You should add all the relevant code like initial component state and the class component using registerUser Commented Jun 25, 2020 at 14:28
  • The is the error : "TS2531: Object is possibly 'null'" Commented Jun 25, 2020 at 14:33
  • Its a type error... see updated answer Commented Jun 25, 2020 at 14:35
  • Well if it says that it might be null then you have to check if it's null or not before using it. Commented Jun 25, 2020 at 14:40

2 Answers 2

1

You will need to use this.emailEl.current according to the react documentation. You might also need to guard against null values to make typescript happy.

import * as React from 'react'

class MyComponent extends React.Component {
    state = { userList: [] }
    emailEl = React.createRef<HTMLInputElement>()
    registerUser = () => {
        const { userList } = this.state
        if (this.emailEl.current) {
            console.log('--------', this.emailEl.current.value)
        }
    }
    render() {
        return <div></div>
    }
}

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

Comments

1

Its a type error, try:

emailEl= createRef<HTMLInputElement | null>();

// or
emailEl= createRef<HTMLInputElement>(null);
registerUser = () => {
    if (emailEl && emailEl.current) {
       // ts knows value not null
       this.emailEl.current.value;
    }
}

See the docs of React.createRef

1 Comment

Add minimal working example. Did you assign the ref? Check the docs.

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.