redux 形式的状态不会改变

State in redux form doesn't change

我的 React 表单中的状态没有改变。 console.log(renderselect) 没有值 - 它显示 undefined.

我尝试了所有方法,但没有任何变化。

import React from "react";
import {Field, reduxForm} from "redux-form";
import {connect} from 'react-redux';
import {renderTextField, renderField, validate, warn, renderselect} from "../../../Components/Forms/renders"
class SyncValidationForm extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      errors: {},
      opencheck: "hallo"
    }
     this.handleSelect = this.handleSelect.bind(this);
  }
  handleSelect = (value) => {
    this.setState({"opencheck": value.target.value});
  };
  render() {
    const {handleSubmit, pristine, reset, submitting, opencheck} = this.props;
    console.log(opencheck);

    return (<form onSubmit={handleSubmit}>
          <div className="box-body">
            <div className="row">
              <div className="col-lg-3">
                <Field className="form" name="favoriteColor" label="Maak eerste een keuze" component={renderselect} options={{
                    "Geen informatie" : 'Geen informatie',
                    'Altijd open' : 'Altijd open'
                  }}
                onChange={this.handleSelect}/>
              </div>
        </div>
      </div>
    </form>);
  }
};

SyncValidationForm = reduxForm({
  form: 'insertstart', enableReinitialize: true,
  warn
})(SyncValidationForm);

const mapStateToProps = state => {
  state => ({
    initialValues: { favoriteColor: 'Geen informatie' }
  })
}

export default connect(mapStateToProps)(SyncValidationForm)

选中的框必须更改状态,但没有任何更改。此外,在第一个渲染中,控制台日志中的 opencheck 没有值。

您将 Redux 状态(映射到 mapStateToProps 中的 React 组件的 props)与内置 React 组件状态 这与 Redux 无关。在继续之前,您可能希望阅读这些链接以充分理解其中的区别:

这两个通常集成的库都调用它们的数据源 "state",这常常让人感到困惑和沮丧,但了解它们的区别并确定您使用的是哪个库对您来说很重要。

如果您想使用 Redux 存储选定状态,您的 handleSelect 函数需要将新值分派到 Redux 存储,通常使用 mapDispatchToProps 来调度一个动作,而不是通过调用 this.setState

如果你想使用React组件状态,那么你需要在你的render()函数中引用this.state.opencheck,而不是this.props. opencheck .