javascript - TypeError: Cannot read property 'focus' of undefined in ReactJS - Stack Overflow

import React, { Component } from "react";import PropTypes from "prop-types";impor

import React, { Component } from "react";
import PropTypes from "prop-types";
import Textarea from "react-textarea-autosize";

class InputSet extends Component {
  constructor(props) {
    super(props);

    this.state = {};
  }


  static propTypes = {
    onChange: PropTypes.func,
    title: PropTypes.string,
    body: PropTypes.string
  };

  ponentDidMount() {
    this.title.focus();
  }

  render() {
    const { onChange, title, body } = this.props;

    return (
      <div>
        <TitleInput
          name="title"
          onChange={onChange}
          placeholder="Title"
          innerRef={ref => (this.title = ref)}
          value={title}
        />
        <StyledTextArea
          minRows={3}
          maxRows={20}
          placeholder="Please enter a note..."
          name="body"
          onChange={onChange}
          value={body}
        />
      </div>
    );
  }
}

export default InputSet;

When I click on a ponent, that error suddenly occurs. And it says TypeError: Cannot read property 'focus' of undefined

The error is occured at ponentDidMount

Can you take the time to help me fix this error?

I can't understand why this error is ing up

import React, { Component } from "react";
import PropTypes from "prop-types";
import Textarea from "react-textarea-autosize";

class InputSet extends Component {
  constructor(props) {
    super(props);

    this.state = {};
  }


  static propTypes = {
    onChange: PropTypes.func,
    title: PropTypes.string,
    body: PropTypes.string
  };

  ponentDidMount() {
    this.title.focus();
  }

  render() {
    const { onChange, title, body } = this.props;

    return (
      <div>
        <TitleInput
          name="title"
          onChange={onChange}
          placeholder="Title"
          innerRef={ref => (this.title = ref)}
          value={title}
        />
        <StyledTextArea
          minRows={3}
          maxRows={20}
          placeholder="Please enter a note..."
          name="body"
          onChange={onChange}
          value={body}
        />
      </div>
    );
  }
}

export default InputSet;

When I click on a ponent, that error suddenly occurs. And it says TypeError: Cannot read property 'focus' of undefined

The error is occured at ponentDidMount

Can you take the time to help me fix this error?

I can't understand why this error is ing up

Share Improve this question asked Dec 25, 2019 at 15:06 Dann1yDann1y 5071 gold badge5 silver badges9 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 4

From what I can see, title is not a class property of your InputSet ponent.

I believe you meant to make use of the React.createRef() API to attach the ref to your React element.

this.title = React.createRef();

On your constructor,

constructor(props) {
  super(props);
  this.state = {};
  this.title = React.createRef();
}

And then,

ponentDidMount() {
  if (this.title.current) {
    this.title.current.focus();
  }
}

You have to add .current like this this.title.current.focus();

Hope this helps

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744246772a4564970.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信