javascript - 使用 div 的两种方式数据绑定(bind)

标签 javascript reactjs redux react-redux

我正在使用带有redux的reactjs。 我创建了一个可编辑的 div 而不是 input 文本字段,但无法接收该值。

所以,在输入文本字段中。有一个名为 onChange 的事件,可让您访问 input 字段中的值类型。 例如 -

handlechange(e){
console.log(e.target.value);  //get the value of textbox then i further save it in state
}
render()
{
  return (
        <input
          onChange={this.handleChange.bind(this)}   
          value={this.state.msgText}
        </>
)}

但是我正在使用可编辑的div,就像这样

<div
          role="textbox"
          ref={function(e){if(e != null) e.contentEditable=true;}}
          title="Type the text"
          //onChange={this.handleChange.bind(this)}
          onKeyUp={this.handleKeyUp.bind(this)}
        >
          {this.state.msgText}
        </div>

所以,在handleKeyUp函数中

   handleKeyUp(e){
        var t = this;
        console.log('test');
        console.log(e);
        console.log(e.target.value);   // I have read ,i can only receive the keycode here,cannot receive value 
console.log(this.state.msgText);   //So i  should receive the value here but i am not
          if(e.which == 13) {
            e.preventDefault();
            //reset the state for clear the div
            t.setState({
              msgText: ""
            });
          }
        }

执行此操作的一种方法是在 div 上添加 id,如下所示 -

<div
          id={"fc-"+ this.props.thread.uid + "-textbox"}
          role="textbox"
          className="ifc-chat-window-textbox-input"
          ref={function(e){if(e != null) e.contentEditable=true;}}
          title="Type the text"
          //onChange={this.handleChange.bind(this)}
          //onKeyUp={this.handleKeyUp.bind(this)}
        >
          {this.state.msgText}
        </div>

然后在componentDidMount函数中

    componentDidMount(){
         var t = this;
         var node = document.getElementById("fc-"+ this.props.thread.uid + "-textbox");
var value = node.textContent;   // I receive the value here
         node.onkeypress = function(event){
           t.setState({
             msgText: node.textContent
            });             });
           if(event.which == 13) {
             event.preventDefault();
             t.sendMsgObject(value , t.props.thread.uid, t.props.thread.name, t.props.thread.color, t.props.actions, t.props.user);
             //reset the state for clear input field
             t.setState({
               msgText: ""
             });
           }
所有这些都工作正常,但我不认为这就是 react 中的工作方式。我希望在不使用 id 来 div 的情况下做到这一点。

最佳答案

你尝试过类似的事情吗?

var handleChange = function(event){
    this.setState({html: event.target.value});
}.bind(this);

return (<ContentEditable html={this.state.html} onChange={handleChange} />);

ContentEditable 类

var ContentEditable = React.createClass({
    render: function(){
        return <div 
            onInput={this.emitChange} 
            onBlur={this.emitChange}
            contentEditable
            dangerouslySetInnerHTML={{__html: this.props.html}}></div>;
    },
    shouldComponentUpdate: function(nextProps){
        return nextProps.html !== this.getDOMNode().innerHTML;
    },
    emitChange: function(){
        var html = this.getDOMNode().innerHTML;
        if (this.props.onChange && html !== this.lastHtml) {

            this.props.onChange({
                target: {
                    value: html
                }
            });
        }
        this.lastHtml = html;
    }
});

关于javascript - 使用 div 的两种方式数据绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36889282/

相关文章:

reactjs - 当我使用react-native创建项目时, 'export default class App extends Component<Props>'中的<Props>是什么意思

react-native - Reducer 在初始化期间返回 undefined 但在 reducer 开关中返回 initialState 作为默认值

javascript - 使用组件 react 路由(PageRenderer)

javascript - Bootstrap 确认 - 对元素属性应用 onConfirm 函数

javascript - Django-Ajax(Jquery)删除功能问题

javascript - 当您使用 jQuery .removeClass() 时,它会在没有该类的元素上运行时返回错误吗?

javascript - 清空下拉选择office-ui-fabric react组件

javascript - UseState 多个初始状态

redux - 处理 Redux Store 数据中的依赖关系

javascript - 将 Redux 状态传递给组件时的最佳实践