小编bal*_*lii的帖子

Apollo GraphQL (React) 跨组件数据共享

我刚开始使用 react-apollo,想知道跨组件共享数据的最佳方式是什么。

例如,我componentA使用grapqlfromreact-apollo来获取一些数据 ( WrappedA)。我还有一些其他组件,componentB在我的 UI 中的其他地方,并且想要使用已经由componentA. 下面附上一个非常简单的例子。

const ComponentA = ({ ...props }) => (
    <div>
        ComponentA...
        id: {props.organisationsData.organisations.id}
    </div>
)

const ComponentB = ({ ...props }) => (
    <div>
    ****** Want to access props.organisationsData *****
    ComponentB...
    </div>
)

const organisationsQuery = gql`
    query organisations {
        organisations {
            id
            name
        }
    }
`

const WrappedA = graphql(organisationsQuery, { name: 'organisationsData' })(WrappedA)

const Container = ({ ...props }) => …
Run Code Online (Sandbox Code Playgroud)

apollo graphql react-apollo apollo-client

5
推荐指数
1
解决办法
1410
查看次数

标签 统计

apollo ×1

apollo-client ×1

graphql ×1

react-apollo ×1