如何将redux-form与reselect一起使用

leu*_*ion 5 reactjs redux redux-form reselect

我想通过redux-form使用reselect从redux获取值。问题是我不知道如何将getFormValues与重新选择结合起来。看来我无法在createSelector中访问状态。因此我无法提出在重新选择中使用redux-form的选择器的方法。

例如:

// How to access the state here?
const getFoo = createSelector(
  [getBar],
  (bar) => {
    // return something
  }
)
Run Code Online (Sandbox Code Playgroud)

redux中的选择器的工作原理如下:

getFormValues('formName')(state);
Run Code Online (Sandbox Code Playgroud)

Sha*_*ews 6

您可能希望将 reselect 与 redux-form 的选择器一起使用(这是您从 redux-form 中获取当前数据的方式)。

您可以在此处了解有关选择器的更多信息....

https://redux-form.com/7.3.0/docs/api/formvalueselector.md/

这里有一个例子......

https://redux-form.com/7.3.0/examples/selectingformvalues/

然后,您可以将 Reselect 选择器与 Redux-form 选择器一起使用,就像这样......

const selector = formValueSelector('myForm');
const mapStateToProps = createStructuredSelector({
  firstValue: (state) => selector(state, 'firstValue')
});
Run Code Online (Sandbox Code Playgroud)

这是另一个使用来自不同 Github 相关主题的示例 https://github.com/erikras/redux-form/issues/1505

const formSelector = formValueSelector('myForm')
const myFieldTitle = (state) => formSelector(state, 'title')
const doSomethingWithTitleSelector = createSelector(myFieldTitle, (title) => {
    return doSomethingWithTitle(title)
})

function doSomethingWithTitle() { ... }

const Form = reduxForm({
    form: 'myForm',
})(TheComponent)

export default connect(
    state => ({
        titleWithSomethingDone: doSomethingWithTitleSelector(state)
    })
)(Form)
Run Code Online (Sandbox Code Playgroud)