如何在 redux 表单中访问 redux store
How to access the redux store within the redux form
我是react-redux世界的新手,如果我遗漏了什么请纠正我:
基本上我试图通过 redux 存储访问主机名并在表单中使用它。截至目前,我只是在 redux 表单中使用主机名引用 window.location,但我想通过 redux 存储/通过任何其他更好的方法访问主机名,以便我可以保持功能纯净?想法?
谢谢
编辑:
import React from 'react'
import { Field, reduxForm } from 'redux-form'
const SimpleForm = (props) => {
const { handleSubmit, pristine, reset, submitting } = props
// goal is to prevent this here and do it via redux store or
// any other options
// so I can avoid external calls inside pure function
const hostName = window.location.hostname;
return (
<form onSubmit={handleSubmit}>
<div>
<label>First Name</label>
<div>
<Field name="firstName" component="input" type="text" placeholder="First Name"/>
</div>
</div>
</form>
)
}
export default reduxForm({
form: 'simple' // a unique identifier for this form
})(SimpleForm)
问题:
我如何将 window 对象存储在 redux 存储中,是否可以通过 redux 存储以 redux 形式访问它们,或者是否会反应路由器帮助?
您应该将表单连接到 Redux 状态。
import { connect } from 'react-redux'
export default reduxForm({
form: 'simple' // a unique identifier for this form
})(connect(mapStateToProps)(SimpleForm)
然后:
const mapStateToProps = state => {
return {
hostName: state.reducerName.hostName
}
}
所以你可以通过this.props.hostName访问它,考虑到之前在reducer中设置了hostName。
如果您通过道具使用 React Router, you can access the history 对象,它也有位置 属性。
前面提到的答案现在对我有用了,
这对我有用,reference to react-redux doc
import { connect } from 'react-redux';
const mapStateToProps = state => {
return {
form: state.form
}
}
ClassName= connect(
mapStateToProps,
mapDispatchToProps
)(ClassName);
export default reduxForm({
form: 'ObjectRepo',
validate: validateObjects
})(ClassName);
我传递的 'validate' 是我的 validation.js,因为我正在使用同步验证(如果有的话,它完全是可选的)
'form' 是我在减速器中使用的状态名称。我的减速器代码,
const allReducers = combineReducers({
form: reduxFormReducer
});
我是react-redux世界的新手,如果我遗漏了什么请纠正我:
基本上我试图通过 redux 存储访问主机名并在表单中使用它。截至目前,我只是在 redux 表单中使用主机名引用 window.location,但我想通过 redux 存储/通过任何其他更好的方法访问主机名,以便我可以保持功能纯净?想法?
谢谢
编辑:
import React from 'react'
import { Field, reduxForm } from 'redux-form'
const SimpleForm = (props) => {
const { handleSubmit, pristine, reset, submitting } = props
// goal is to prevent this here and do it via redux store or
// any other options
// so I can avoid external calls inside pure function
const hostName = window.location.hostname;
return (
<form onSubmit={handleSubmit}>
<div>
<label>First Name</label>
<div>
<Field name="firstName" component="input" type="text" placeholder="First Name"/>
</div>
</div>
</form>
)
}
export default reduxForm({
form: 'simple' // a unique identifier for this form
})(SimpleForm)
问题:
我如何将 window 对象存储在 redux 存储中,是否可以通过 redux 存储以 redux 形式访问它们,或者是否会反应路由器帮助?
您应该将表单连接到 Redux 状态。
import { connect } from 'react-redux'
export default reduxForm({
form: 'simple' // a unique identifier for this form
})(connect(mapStateToProps)(SimpleForm)
然后:
const mapStateToProps = state => {
return {
hostName: state.reducerName.hostName
}
}
所以你可以通过this.props.hostName访问它,考虑到之前在reducer中设置了hostName。
如果您通过道具使用 React Router, you can access the history 对象,它也有位置 属性。
前面提到的答案现在对我有用了,
这对我有用,reference to react-redux doc
import { connect } from 'react-redux';
const mapStateToProps = state => {
return {
form: state.form
}
}
ClassName= connect(
mapStateToProps,
mapDispatchToProps
)(ClassName);
export default reduxForm({
form: 'ObjectRepo',
validate: validateObjects
})(ClassName);
我传递的 'validate' 是我的 validation.js,因为我正在使用同步验证(如果有的话,它完全是可选的)
'form' 是我在减速器中使用的状态名称。我的减速器代码,
const allReducers = combineReducers({
form: reduxFormReducer
});