简体   繁体   English

React Redux 使用 HOC 和连接组件

[英]React Redux use HOC with connected component

I am in the middle of my first React Native project.我正在进行我的第一个 React Native 项目。 I would like to create a HOC that deals purely with syncing data from an api.我想创建一个纯粹处理从 api 同步数据的 HOC。 This would then wrap all my other components.这将包装我所有的其他组件。

If I am correct my DataSync component would enhance all other components by doing the following in the export statement:如果我是正确的,我的DataSync组件将通过在导出语句中执行以下操作来增强所有其他组件:

export default DataSync(SomeOtherComponent);

The concept I am struggling with is that SomeOtherComponent also depends on the React Redux Connect method for retrieving other redux state.我挣扎的概念是SomeOtherComponent还依赖于 React Redux Connect 方法来检索其他 redux 状态。 My question is how can I use both together?我的问题是如何同时使用两者? Something like this?像这样的东西?

export default DataSync(connect(mapStateToProps, mapDispatchToProps)(SomeOtherComponent));

I may have completely misunderstood the concept here so I would really appreciate some pointers我可能完全误解了这里的概念,所以我真的很感激一些建议

EDIT编辑

To explain further:进一步解释:

My DataSync HOC would purely handle the syncing of data between the app and would be the top level component.我的 DataSync HOC 将纯粹处理应用程序之间的数据同步,并将成为顶级组件。 It would need access to auth state and would set the data in Redux (in this case orders) for all other components.它需要访问身份验证状态,并将在 Redux 中为所有其他组件设置数据(在本例中为订单)。

Components nested within the DataSync HOC need access to the retrieved data, routes and they in turn create state (orders) that must be synced back to the server periodically.嵌套在 DataSync HOC 中的组件需要访问检索到的数据、路由,然后它们又会创建必须定期同步回服务器的状态(订单)。

Here is a simple example how it works这是一个简单的例子,它是如何工作的

const AppWrapper = MainComponent =>
  class extends Component{
    componentWillmount(){
      this.props.fetchSomething()
    }
    componentDidUnmount(){
      this.props.push('/')
    }
    render(){
      return (
        <div>
          {this.props.fetchedUsers === 'undefined' ? 
            <div> Do something while users are not fetched </div> :
            <MainComponent {...this.props}/>}
        </div>
      )
    }
  }



const App = ({users}) => {
  // just example, you can do whatever you want
  return <div>{JSON.stringify(users)}</div>
};

// mapStateToProps will be in HOC -> Wrapper
// mapDispatchToProps will be in HOC -> Wrapper

/* you may use DataSync as you want*/
export default connect(mapStateToProps, mapDispatchToProps)(AppWrapper(App))

Useful HOC link有用的HOC链接

May be this is what you wanted:可能这就是你想要的:

DataSync.js数据同步.js

export default connect(mapStateToProps, mapDispatchToProps)(DataSync);

SomeOtherComponent.js SomeOtherComponent.js

export default DataSync(connect(mapStateToProps, mapDispatchToProps)(SomeOtherComponent));

Use connect on your child components as well.也在您的子组件上使用connect Here is WHY这是为什么

Yes, connect is also HOC and you can nest them arbitrary since a HOC returns a component.是的, connect也是HOC ,你可以任意嵌套它们,因为HOC返回一个组件。

HOC(HOC(...(Component)...)) is OK. HOC(HOC(...(Component)...))


However, I think what you might need is connect(...)(DataSync(YourComponent)) instead of DataSync(connect(...)(YourComponent)) so that DataSync could also access state / props if needed.但是,我认为您可能需要的是connect(...)(DataSync(YourComponent))而不是DataSync(connect(...)(YourComponent))以便DataSync也可以在需要时访问state / props It really depends on the use case.这真的取决于用例。

I had a very straight forward use case.我有一个非常直接的用例。 I wanted to connect my HOC with redux store.我想将我的 HOC 与 redux store 连接起来。 In short I wanted to wrap my HOC with redux connect method.简而言之,我想用 redux connect方法包装我的 HOC。

// The HOC
const withMyHoc = (ReduxWrappedComponent) => props => <ReduxWrappedComponent {...props} />

// redux props
const mapStateToProps = (state) => ({});
const mapDispatchToProps = (dispatch) => ({});

// This is important
export default (WrappedComponent) => 
connect(
  mapStateToProps, 
  mapDispatchToProps
)(withMyHoc(WrappedComponent));

There are two many answers in this thread.这个线程中有两个答案。 All of them helped me.他们都帮助了我。 Just putting down what actually worked in my case.只是记下在我的情况下实际有效的内容。

I use and like the same approach that @The Reason mentioned.我使用并喜欢@The Reason 提到的相同方法。 The only problem here is that if you map your actions you won't have dispatch() available.这里唯一的问题是,如果您映射您的操作,您将无法使用 dispatch()。

The way how I managed to make it work in case someone is facing the same problem was the following.如果有人面临同样的问题,我如何设法使其工作的方式如下。

const ConnectedComponentWithActions = connect(
  () => { return {}; },
  { myAction },
)(ViewComponent);

export default connect(
  state => state,
  null,
)(withPreFetch(firstLoadAction, ConnectedComponentWithActions));

Where withPreFetch(firstLoadAction, ConnectedComponentWithActions) is the HOC accepting an action to be dispatched.其中withPreFetch(firstLoadAction, ConnectedComponentWithActions)是接受要分派的动作的 HOC。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM