【问题标题】:onChange function doesn't work for input while using ReduxonChange 函数在使用 Redux 时对输入不起作用
【发布时间】:2017-11-07 10:00:13
【问题描述】:

我正在编写一个简单的代码,它将生成一个带有数据的输入框。当我更改输入框中的数据时,应该更新数据库。我正在使用 onChange 函数将触发一个动作来实现这一点,但 onChange 似乎根本没有触发。浏览器一直显示“警告:表单 propType 失败:您向表单提供了 value 道具..”。

以下是我的代码:

import React, {Component} from 'react';
import {connect} from 'react-redux';
import {bindActionCreators} from 'redux';
import {handleChange} from '../actions/handleChange';


class NoteSelected extends Component {
   render() {   // onChange to be added
        const inputStyle = {width: "100%"};
        if(!this.props.noteSelected)
            return (<h4>Select a note..</h4>)
        return (
            <div className="col-sm-9 col-md-6 col-lg-8" >
            <h4>{this.props.noteSelected.heading}</h4>
            <input key={this.props.noteSelected.id} id={this.props.noteSelected.id} 
             value={this.props.noteSelected.text} onChange={this.props.handleChange} style={inputStyle} />
        </div>
        )
    }
}

function mapStateToProps (state) {
    return {
        noteSelected: state.noteSelected
    }
}

function mapDispatchToProps(dispatch) {
    return bindActionCreators({handleChange: handleChange}, dispatch)
}

export default connect (mapStateToProps, mapDispatchToProps) (NoteSelected);

【问题讨论】:

标签: reactjs redux react-redux redux-form


【解决方案1】:

问题其实和这个链接一样:Failed form propType: You provided a `value` prop to a form field without an `onChange` handler

只需将value 更改为defaultValueplaceholder 即可解决问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-22
    • 1970-01-01
    • 2021-01-18
    • 2021-09-26
    • 1970-01-01
    • 2018-06-08
    • 1970-01-01
    相关资源
    最近更新 更多