最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Graphql, react-apollo how to transfer variable to query at loading component state - Stack Overflow

programmeradmin5浏览0评论

I have a simple react ponent that must load data from server when user ask it. Problem is that i don't know how to transfer dynamic variable speakerUrl and access it in before ponent load state. Sure i can access it from this.props.params, but ponent is not loaded and i can't access it when i make a graphql query. Query - QuerySpeaker is working fine when i manualy set url variable.

Router

<Route path="/speaker/:speakerUrl" ponent={SpeakerPage} />

Component - SpeakerPage

import React from 'react';
import { graphql } from 'react-apollo';

import { QuerySpeaker } from '../redux/graphql/querys';

class SpeakerPage extends React.Component {
    render( ) {
        console.log( this.props );
        return (
            <div className="ui container">
                <div className="ui grid">
                    <div className="row">
                        Hello
                    </div>
                </div>
            </div>
        )
    }
}

export default graphql(QuerySpeaker, {
     options: ({ url }) => ({ variables: { url } }) <- here is my problem, how can i set this url variable?
})( SpeakerPage );

I have a simple react ponent that must load data from server when user ask it. Problem is that i don't know how to transfer dynamic variable speakerUrl and access it in before ponent load state. Sure i can access it from this.props.params, but ponent is not loaded and i can't access it when i make a graphql query. Query - QuerySpeaker is working fine when i manualy set url variable.

Router

<Route path="/speaker/:speakerUrl" ponent={SpeakerPage} />

Component - SpeakerPage

import React from 'react';
import { graphql } from 'react-apollo';

import { QuerySpeaker } from '../redux/graphql/querys';

class SpeakerPage extends React.Component {
    render( ) {
        console.log( this.props );
        return (
            <div className="ui container">
                <div className="ui grid">
                    <div className="row">
                        Hello
                    </div>
                </div>
            </div>
        )
    }
}

export default graphql(QuerySpeaker, {
     options: ({ url }) => ({ variables: { url } }) <- here is my problem, how can i set this url variable?
})( SpeakerPage );
Share Improve this question asked Dec 7, 2016 at 20:36 SLISLI 75311 silver badges29 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 18

Based on the react-apollo documentation, the argument passed to the options function is the props object passed to the ponent. When react-router renders your ponent, it passes it the params as a prop. That means that params should be a property of the object passed to the options function.

export default graphql(QuerySpeaker, {
  options: (props) => ({ variables: { url: props.match.params.speakerUrl } })
})( SpeakerPage );

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论