简体   繁体   English

React 表单输入不会让我改变值

[英]React form input won't let me change value

I have a component in a React class in my Laravel project which is a simple form with one input field.我在 Laravel 项目的 React 类中有一个组件,它是一个带有一个输入字段的简单表单。 It houses a phone number which I have retrieved from the database and passed back through the reducer and into the component as a prop.它包含一个我从数据库中检索到的电话号码,并通过减速器传回并作为道具传递到组件中。 Using this, I have passed it through to the module as a prop which then populates the field with the currently saved value:使用这个,我将它作为一个 prop 传递给模块,然后用当前保存的值填充该字段:

<OutOfOfficeContactNumberForm
    show={props.showOutOfOffice}
    value={props.outOfOfficeNumber}
    handleChange={console.log("changed")}
/>

I have a handleChange on here which is supposed to fire a console log, but it only ever displays on page load.我在这里有一个handleChange应该触发控制台日志,但它只在页面加载时显示。 Here is my form module class:这是我的表单模块类:

class OutOfOfficeContactNumberForm extends React.Component {
    render() {
        const { show, value, handleChange } = this.props;

        if(!show) return null;

        return (
            <div>
                <p>
                    Please supply an Out of Office contact number to continue.
                </p>
                <InputGroup layout="inline">
                    <Label layout="inline" required={true}>Out of Office Contact Number</Label>
                    <Input onChange={handleChange} value={value} layout="inline" id="out-of-office-number" name="out_of_office_contact_number" />
                </InputGroup>
            </div>
        );
    }
}

export default (CSSModules(OutOfOfficeContactNumberForm, style));

The form is embedded in my parent component, as follows:表单嵌入在我的父组件中,如下所示:

return (
    <SectionCategoriesSettingsForm
        isSubmitting={this.state.isSubmitting}
        page={this.props.page}
        show={this.props.show}
        categories={this.props.categories}
        submitSectionCategoriesSettings={this._submit.bind(this, 'add')}
        updateSelectedCategories={this._updateSelectedCategories.bind(this)}
        selectedCategoryIds={this.state.selectedCategoryIds}
        storedUserCategories={this.props.selectedCategories}
        outOfOfficeNumber={this.state.outOfOfficeNumber}
        onUpdateContactNumber={this._updateContactNumber.bind(this)}
    />
);

In my componentWillReceiveProps() function, I set the state as follows:在我的componentWillReceiveProps()函数中,我将状态设置如下:

if (nextProps.selectedCategories && nextProps.selectedCategories.length > 0) {
    this.setState({
        outOfOfficeNumber: nextProps.outOfOfficeNumber,
        selectedCategoryIds: nextProps.selectedCategories.map(c => c.id)
    });
}

I'm pretty sure the reason it's not changing is because it's pre-loaded from the state which doesn't change - but if I cannot edit the field how can I get it to register a change?我很确定它没有改变的原因是因为它是从不变的状态预加载的 - 但是如果我无法编辑该字段,我怎样才能让它注册更改?

EDIT : Just to clarify there are also checkboxes in this form for the user to change their preferences, and the data retrieved for them is set the same way but I am able to check and uncheck those no problem编辑:只是为了澄清此表单中还有复选框供用户更改他们的首选项,并且为他们检索的数据设置相同,但我可以检查和取消选中那些没问题

Changes:变化:

1- onChange expect a function and you are assigning a value that's why, put the console statement inside a function and pass that function to OutOfOfficeContactNumberForm component , like this: 1- onChange期望一个函数并且您正在分配一个值,这就是为什么,将控制台语句放在函数中并将该函数传递给OutOfOfficeContactNumberForm组件,如下所示:

handleChange={() => console.log("changed")}

2- You are using controlled component (using the value property), so you need to update the value inside onChange function otherwise it will not allow you to change means input values will not be not reflect in ui. 2-您正在使用受控组件(使用 value 属性),因此您需要更新 onChange 函数内的值,否则它将不允许您更改意味着输入值不会反映在 ui 中。

Check example:检查示例:

 class App extends React.Component { state = { input1: '', input2: '', } onChange = (e) => this.setState({ input2: e.target.value }) render() { return( <div> Without updating value inside onChange <input value={this.state.input1} onChange={console.log('value')} /> <br /> Updating value in onChange <input value={this.state.input2} onChange={this.onChange} /> </div> ) } } ReactDOM.render(<App />, document.getElementById('app'))
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> <div id='app' />

我认为最好的方法是当您从数据库中获取数据时将其放入状态并将状态传递给输入并记住是否要查看输入的输入更改,使用函数来处理更改并且该函数应该更改状态值。

class payloadcontainer extends Component {
     constructor(props) {
         super(props)     
         this.state = {
              number:1
         }
     }    

    render() {

        return (
            <div>
                <input value={this.state.number} onChange={(e)=>this.setState({number:e.target.value})}></input>
                <button onClick={()=>this.props.buyCake(this.state.number)}><h3>buy {this.state.number} cake </h3></button>
            </div>
        )
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM