4.1.1
由于Apollo,该组件通过GraphQL查询数据库。它是一个GraphQL容器。
它使用compose
库中的react-apollo
,以便以可读的方式同时使用multiple enhanchers。
示例组件:
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { graphql, compose, withApollo } from 'react-apollo';
import { myQuery } from './graphqlQuery';
class MyComponent extends Component {
render() {
const { exampleProp } = this.props;
return null;
}
}
function mapStateToProps(state, ownProps) {
// I expect to see ownProps.exampleProp here but it is undefined
console.log(ownProps.exampleProp);
}
export default compose(
withApollo,
connect(mapStateToProps),
graphql(myQuery),
)(MyComponent);
ownProps应包含传递给组件as stated here的道具 像:
Object = {
client: ApolloClient
exampleProp: "propcontent", // <-- this is going to be lost
history: Object
location: Object
match: Object
staticContext: undefined
__proto__: Object
}
而ownProps只包含这个:
Object = {
client: ApolloClient
history: Object
location: Object
match: Object
staticContext: undefined
__proto__: Object
}
组件应该具有来自父项和GraphQL响应的所有道具,但在这种情况下包括exampleProp
。