javascript - onFocus 和 onBlur 不会在 React 中渲染

标签 javascript reactjs dom-events

我有以下代码

<ElementsBasket
  name="nextActionDate"
  data={this.props.newLead.get("actions").get("nextActionDate")}
>
  <div className="form-group">
    <span className="glyphicon glyphicon-calendar">Calendar </span>
    <span className="glyphicon glyphicon-triangle-bottom"></span>

    <input
      type="text"
      className="form-control"
      onFocus={(this.type = "date")}
      onBlur={(this.type = "text")}
      placeholder="Enter your date here."
      value={this.props.newLead.get("actions").get("nextActionDate")}
      onChange={onFieldChanged.bind(this, "actions.nextActionDate")}
    />
  </div>
</ElementsBasket>;

在输入标记中,我试图默认在输入字段中显示占位符文本,单击时我希望将类型更改为日期。问题似乎出在单击 Chrome 上的检查元素时。它不会显示 onFocusonBlur

P/S:即使是onClick似乎也有同样的问题

最佳答案

这是您要找的吗?

http://codepen.io/comakai/pen/WvzRKp?editors=001

var Foo = React.createClass({
  getInitialState: function () {

    return {
      type: 'text'
    };
  },
  onFocus: function () {

    this.setState({
      type: 'date'
    });
  },
  onBlur: function () {

    this.setState({
      type: 'text'
    });
  },
  render: function () {

    return(
      <input 
        type={ this.state.type } 
        onFocus={ this.onFocus } 
        onBlur={ this.onBlur } 
        placeholder="Enter your date here."
      />
    )
  }
});

React.render(<Foo/>, document.body);

正如我上面所评论的,render 方法会在每次状态更改时第一次触发(并且如果 shouldComponentRender 返回 true 以防其实现):

https://facebook.github.io/react/docs/component-specs.html

关于javascript - onFocus 和 onBlur 不会在 React 中渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31247214/

相关文章:

javascript - 如何在 Angular 5 中使用 Material 步进器

javascript - 当鼠标悬停时,Chartjs 显示标签和单位统计

javascript - 查找输入字段中编辑文本的位置

javascript - 带有警报的 javascript 问题未显示

javascript - 如何让其他网站可以访问我的 iframe 的 javascript?

reactjs - 在 React TS 组件中,如何传递未在界面中定义的可选 Prop ?

javascript - 似乎无法返回一个值,只有一个 promise

javascript - 将 create-react-app 从 javascript 迁移到 typescript

javascript - 放下三个物体后触发图像变化

javascript - z-index 和 Javascript 事件