Redux: 延迟文件上传...文件对象放在哪里?

创建于 2016-07-18  ·  3评论  ·  资料来源: reduxjs/redux

我正在寻找有关处理文件上传的建议。 我有一个登陆页面,用户可以在其中拖放文件,然后在文件上传开始之前提示注册。 我需要将拖动的文件对象保存在某处,但由于 Redux 状态只应该保存普通的 JS 对象,我不确定将该文件对象放在哪里。 我可以将它保持在某些 React 状态/道具中,但我必须确保它最终传递给正确的组件,等等。

最有用的评论

@olalonde状态应该是可序列化的建议是一个温和的建议 - 它不是

即使您随后序列化您的状态 - File描述符也将序列化为一个对象对象,因此您不会收到序列化错误。

所有3条评论

一种可能的解决方案是使用 store.subscribe 监听商店的状态更改,但我不确定如何使用connect(...)访问 store.subscribe 。 也许this.context.store.subscribe()

@olalonde状态应该是可序列化的建议是一个温和的建议 - 它不是

即使您随后序列化您的状态 - File描述符也将序列化为一个对象对象,因此您不会收到序列化错误。

谢谢。 我最终用以下函数包装了我的上传动作创建者:

waitIntroMessageShowedActionCreator.js

// delays execution of `fn` until `state.session.user.ui.introMessageShowed === true`

export const initWaitIntroMessageShowed = ({ getState, subscribe }) => (fn) => () => (
  new Promise((resolve, reject) => {
    const handleChange = () => {
      const state = getState()
      if (!state.session.user.ui) return null
      if (!state.session.user.ui.introMessageShowed) return null
      /* eslint-disable no-use-before-define */
      unsubscribe()
      return Promise
        .resolve()
        .then(() => fn())
        .catch(reject)
        .then(resolve)
    }
    const unsubscribe = subscribe(handleChange)
    handleChange()
  })
)

在我的组件中:

class DragDropSignup extends Component {
  constructor(props) {
    super(props)
    const waitIntroMessageShowed = initWaitIntroMessageShowed(this.props.store)
    this.onSignupSuccess = waitIntroMessageShowed(this.onSignupSuccess.bind(this))
  }
 // ...
}
// ...

// pass store object as prop
const connectStore = () => (WrappedComponent) => {
  const ConnectStore = (props, context) => (
    <WrappedComponent store={context.store} {...props} />
  )
  ConnectStore.contextTypes = { store: React.PropTypes.object }
  return ConnectStore
}

export default connectStore()(connect()(DragDropSignup))

在这里发布以防它对某人有用

此页面是否有帮助?
0 / 5 - 0 等级