Reactjs 防止在页面加载时进行graphql查询
每次我打开我的页面,我的页面都会对graphql进行查询。如何防止页面加载时进行查询,因为我只希望在用户单击搜索按钮时进行查询 以下是我的组件和graphql代码:Reactjs 防止在页面加载时进行graphql查询,reactjs,graphql,react-apollo,Reactjs,Graphql,React Apollo,每次我打开我的页面,我的页面都会对graphql进行查询。如何防止页面加载时进行查询,因为我只希望在用户单击搜索按钮时进行查询 以下是我的组件和graphql代码: import React from 'react'; import PropTypes from 'prop-types'; import gql from 'graphql-tag'; import { graphql } from 'react-apollo'; constructor(props) { supe
import React from 'react';
import PropTypes from 'prop-types';
import gql from 'graphql-tag';
import { graphql } from 'react-apollo';
constructor(props) {
super(props);
this.state = {
value: '',
};
this.handleChange = this.handleChange.bind(this);
}
handleChange(event) {
this.setState({ value: event.target.value });
}
submit() {
this.props.searchData.refetch({ asset_type: this.state.value})
}
render() {
<div>
<Input value={this.state.value} onChange={this.handleChange} />
<button onClick={() => { this.submit(); }} >
<span>Search</span>
</button>
</div>
// other html element
}
PlacementSearch.propTypes = {
searchData: PropTypes.shape({
refetch: PropTypes.function,
loading: PropTypes.bool.isRequired,
}).isRequired,
};
const queryVariable = gql`
query RootQuery($asset_type: String) {
am {
assets(asset_type: $asset_type) {
data {
uuid
label
asset_type
description
}
}
}
}
`;
export default graphql(queryVariable, {
name: 'searchData',
skip: props => props.url.stopQuery,
options: { variables: { asset_type: '' } },
})(PlacementSearch);
如何检查页面是否已完成加载,然后将skip
更改为false
。我甚至不确定使用skip
是否采取了正确的方法
感谢您的帮助。提前谢谢。我也有同样的问题。你有没有想过如何防止页面加载时触发查询?我也有同样的问题。您是否想过如何防止在页面加载时触发查询?
props.url.stopQuery = true;
<PlacementSearch url={props.url} />