【问题标题】:Keep Redux form values after page reload.(without submit form)页面重新加载后保留 Redux 表单值。(不提交表单)
【发布时间】:2018-09-24 12:27:56
【问题描述】:
页面重新加载后redux表单页面设置值。我将状态保存在本地存储中。我现在所拥有的是在我点击输入字段之前页面重新加载值没有显示。为了实现这一点,我应该使用哪种方法?任何教都会很好。
video
export default reduxForm({
form: 'checkoutForm',
keepDirtyOnReinitialize: true,
enableReinitialize: true,
})(PaymentOptions);
【问题讨论】:
标签:
reactjs
react-redux
local-storage
redux-form
【解决方案1】:
如果我正确理解手头的问题,我有一个可能对您有用的建议。
创建一个获取本地存储的动作创建者
export const getLocalStorageData = () => dispatch => {
const storage = localStorage.getItem('form_data')
dispatch(...update store here)
}
尝试连接initialValues
CheckoutForm = connect(
state => ({
initialValues: state.data // pull initial values from reducer
}),
{ data: getLocalStorageData } // bind getLocalStorageData action creator
)(CheckoutForm)
redux-form docs on initialValues供您参考。