Set State using query component React apollo Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Setting state in the Query component of react-apolloLoop inside React JSXProgrammatically navigate using react routerHow get value datapicker in react toobox custom?Apollo-client (react) - Update on create mutation - “Can't find field Fund() on object (ROOT_QUERY)”Query data with GraphQL in React with data from a previous queryReact Apollo returning Null when Graphiql and Altair succeed with exact same mutation?Organization structure for fragment composition in large react-apollo appsReact Apollo using Named VariableGraphQL Apollo React: Creating a resource with mutation and updating UI flowUpdate local config based on query result in react-apollo

Is openssl rand command cryptographically secure?

What does 丫 mean? 丫是什么意思?

My mentor says to set image to Fine instead of RAW — how is this different from JPG?

How can I prevent/balance waiting and turtling as a response to cooldown mechanics

I can't produce songs

What is the difference between a "ranged attack" and a "ranged weapon attack"?

what is the log of the PDF for a Normal Distribution?

Was Kant an Intuitionist about mathematical objects?

Are the endpoints of the domain of a function counted as critical points?

GDP with Intermediate Production

In musical terms, what properties are varied by the human voice to produce different words / syllables?

"klopfte jemand" or "jemand klopfte"?

If Windows 7 doesn't support WSL, then what is "Subsystem for UNIX-based Applications"?

Why complex landing gears are used instead of simple,reliability and light weight muscle wire or shape memory alloys?

Relating to the President and obstruction, were Mueller's conclusions preordained?

Is it dangerous to install hacking tools on my private linux machine?

Flight departed from the gate 5 min before scheduled departure time. Refund options

Is there hard evidence that the grant peer review system performs significantly better than random?

Does the Mueller report show a conspiracy between Russia and the Trump Campaign?

Nose gear failure in single prop aircraft: belly landing or nose-gear up landing?

Test print coming out spongy

Why is std::move not [[nodiscard]] in C++20?

A `coordinate` command ignored

What would you call this weird metallic apparatus that allows you to lift people?



Set State using query component React apollo



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!Setting state in the Query component of react-apolloLoop inside React JSXProgrammatically navigate using react routerHow get value datapicker in react toobox custom?Apollo-client (react) - Update on create mutation - “Can't find field Fund() on object (ROOT_QUERY)”Query data with GraphQL in React with data from a previous queryReact Apollo returning Null when Graphiql and Altair succeed with exact same mutation?Organization structure for fragment composition in large react-apollo appsReact Apollo using Named VariableGraphQL Apollo React: Creating a resource with mutation and updating UI flowUpdate local config based on query result in react-apollo



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I have used same form to create and update account(module). Create is working fine but in update mode I am not able to set form field value using Set State methods. I have used query component on render methods and setstate not working on rendor method.



import Mutation from "react-apollo";
import Query from "react-apollo";
import gql from "graphql-tag";
import React, Component from "react";
import Router from "next/router";
import Joi from "joi-browser";

const CREATE_ACCOUNT = gql`
mutation CreateAccount(
$name: String
$phone_office: String
$website: String
)
createAccount(name: $name, phone_office: $phone_office, website:
$website)
name
phone_office
website


`;



export const allAccountbyidQuery = gql`
query account($id: String)
account(id: $id)
id
name
phone_office
website

;
const schema =
name: Joi.string()
.required()
.error(errors =>
return
message: "Name is required!"
;
),
phone_office: Joi.string()
.required()
.error(errors =>
return
message: "Phone Number is required!"
;
),
website: Joi.string()
.required()
.error(errors =>
return
message: "Website is required!"
;
)
;


Main class component



class CreateAccountModule extends React.Component 
static async getInitialProps( query )
const id = query;
return id ;

constructor(props)
super();
this.state =
isFirstRender: true,
name: "",
phone_office: "",
website: ""
;


handleChange = event =>
console.log("hello");
const name, value = event.target;
this.setState( [name]: value );

;
validate(name, phone_office, website)
let errors = "";
const result = Joi.validate(

name: name,
phone_office: phone_office,
website: website
,
schema
);
if (result.error)
errors = result.error.details[0].message;

return errors;

setName = name =>
if (this.state.isFirstRender)
this.setState( name, isFirstRender: false );

;

render()
let input;
const errors = this.state;
console.log(this.props);

const allAccountbyidQueryVars =
id: this.props.id
;

//console.log(allAccountbyidQueryVars);
return (
<Query
query=allAccountbyidQuery
variables=allAccountbyidQueryVars
onCompleted=data => this.setName(data.account.name)
>
( data, loading, error ) =>
<CreateAccountModule account=data.account />;

return (
<Mutation mutation=CREATE_ACCOUNT>
(createAccount, loading, error ) => (
<div>
<form
onSubmit=e =>
e.preventDefault();
const errors = this.validate(
e.target.name.value,
e.target.phone_office.value,
e.target.website.value
);
if (errors)
this.setState( errors );
return;

if (!errors)
let accountres = createAccount(
variables:
name: e.target.name.value,
phone_office: e.target.phone_office.value,
website: e.target.website.value

).then(() => Router.push("/"));
input.value = "";


>
errors && <p>errors</p>
<input
type="text"
name="name"
id="name"
placeholder="Name"
value=this.state.name
onChange=this.handleChange
/>
<input
name="phone_office"
id="phone_office"
placeholder="Phone Number"
//value=data.account.phone_office
//value="123456"
onChange=this.handleChange
/>
<input
name="website"
id="website"
placeholder="Website"
//value=data.account.website
onChange=this.handleChange
/>
<button type="submit">Add Account</button>
<button type="button">Cancel</button>
</form>
loading && <p>Loading...</p>
error && <p>Error :( Please try again</p>
</div>
)
</Mutation>
);

</Query>
);



export default CreateAccountModule;
`


I have tried with props but get props data in apollo state. anyone please suggest possible solution to fix this issue.










share|improve this question
























  • Asked and answered: Setting state in the Query component of react-apollo

    – Daniel Rearden
    Mar 22 at 12:05












  • Thanks i have already check this answer before i asked but How to Define QUERY component outside main class? Thanks

    – Rigal
    Mar 22 at 13:04











  • Like the answer shows, you need to wrap your Mutation component in another component. You cannot maintain the form state inside CreateAccountModule if you're going to initialize it using the query result. Alternatively, move your Query component outside of CreateAccountModule.

    – Daniel Rearden
    Mar 22 at 13:09

















0















I have used same form to create and update account(module). Create is working fine but in update mode I am not able to set form field value using Set State methods. I have used query component on render methods and setstate not working on rendor method.



import Mutation from "react-apollo";
import Query from "react-apollo";
import gql from "graphql-tag";
import React, Component from "react";
import Router from "next/router";
import Joi from "joi-browser";

const CREATE_ACCOUNT = gql`
mutation CreateAccount(
$name: String
$phone_office: String
$website: String
)
createAccount(name: $name, phone_office: $phone_office, website:
$website)
name
phone_office
website


`;



export const allAccountbyidQuery = gql`
query account($id: String)
account(id: $id)
id
name
phone_office
website

;
const schema =
name: Joi.string()
.required()
.error(errors =>
return
message: "Name is required!"
;
),
phone_office: Joi.string()
.required()
.error(errors =>
return
message: "Phone Number is required!"
;
),
website: Joi.string()
.required()
.error(errors =>
return
message: "Website is required!"
;
)
;


Main class component



class CreateAccountModule extends React.Component 
static async getInitialProps( query )
const id = query;
return id ;

constructor(props)
super();
this.state =
isFirstRender: true,
name: "",
phone_office: "",
website: ""
;


handleChange = event =>
console.log("hello");
const name, value = event.target;
this.setState( [name]: value );

;
validate(name, phone_office, website)
let errors = "";
const result = Joi.validate(

name: name,
phone_office: phone_office,
website: website
,
schema
);
if (result.error)
errors = result.error.details[0].message;

return errors;

setName = name =>
if (this.state.isFirstRender)
this.setState( name, isFirstRender: false );

;

render()
let input;
const errors = this.state;
console.log(this.props);

const allAccountbyidQueryVars =
id: this.props.id
;

//console.log(allAccountbyidQueryVars);
return (
<Query
query=allAccountbyidQuery
variables=allAccountbyidQueryVars
onCompleted=data => this.setName(data.account.name)
>
( data, loading, error ) =>
<CreateAccountModule account=data.account />;

return (
<Mutation mutation=CREATE_ACCOUNT>
(createAccount, loading, error ) => (
<div>
<form
onSubmit=e =>
e.preventDefault();
const errors = this.validate(
e.target.name.value,
e.target.phone_office.value,
e.target.website.value
);
if (errors)
this.setState( errors );
return;

if (!errors)
let accountres = createAccount(
variables:
name: e.target.name.value,
phone_office: e.target.phone_office.value,
website: e.target.website.value

).then(() => Router.push("/"));
input.value = "";


>
errors && <p>errors</p>
<input
type="text"
name="name"
id="name"
placeholder="Name"
value=this.state.name
onChange=this.handleChange
/>
<input
name="phone_office"
id="phone_office"
placeholder="Phone Number"
//value=data.account.phone_office
//value="123456"
onChange=this.handleChange
/>
<input
name="website"
id="website"
placeholder="Website"
//value=data.account.website
onChange=this.handleChange
/>
<button type="submit">Add Account</button>
<button type="button">Cancel</button>
</form>
loading && <p>Loading...</p>
error && <p>Error :( Please try again</p>
</div>
)
</Mutation>
);

</Query>
);



export default CreateAccountModule;
`


I have tried with props but get props data in apollo state. anyone please suggest possible solution to fix this issue.










share|improve this question
























  • Asked and answered: Setting state in the Query component of react-apollo

    – Daniel Rearden
    Mar 22 at 12:05












  • Thanks i have already check this answer before i asked but How to Define QUERY component outside main class? Thanks

    – Rigal
    Mar 22 at 13:04











  • Like the answer shows, you need to wrap your Mutation component in another component. You cannot maintain the form state inside CreateAccountModule if you're going to initialize it using the query result. Alternatively, move your Query component outside of CreateAccountModule.

    – Daniel Rearden
    Mar 22 at 13:09













0












0








0








I have used same form to create and update account(module). Create is working fine but in update mode I am not able to set form field value using Set State methods. I have used query component on render methods and setstate not working on rendor method.



import Mutation from "react-apollo";
import Query from "react-apollo";
import gql from "graphql-tag";
import React, Component from "react";
import Router from "next/router";
import Joi from "joi-browser";

const CREATE_ACCOUNT = gql`
mutation CreateAccount(
$name: String
$phone_office: String
$website: String
)
createAccount(name: $name, phone_office: $phone_office, website:
$website)
name
phone_office
website


`;



export const allAccountbyidQuery = gql`
query account($id: String)
account(id: $id)
id
name
phone_office
website

;
const schema =
name: Joi.string()
.required()
.error(errors =>
return
message: "Name is required!"
;
),
phone_office: Joi.string()
.required()
.error(errors =>
return
message: "Phone Number is required!"
;
),
website: Joi.string()
.required()
.error(errors =>
return
message: "Website is required!"
;
)
;


Main class component



class CreateAccountModule extends React.Component 
static async getInitialProps( query )
const id = query;
return id ;

constructor(props)
super();
this.state =
isFirstRender: true,
name: "",
phone_office: "",
website: ""
;


handleChange = event =>
console.log("hello");
const name, value = event.target;
this.setState( [name]: value );

;
validate(name, phone_office, website)
let errors = "";
const result = Joi.validate(

name: name,
phone_office: phone_office,
website: website
,
schema
);
if (result.error)
errors = result.error.details[0].message;

return errors;

setName = name =>
if (this.state.isFirstRender)
this.setState( name, isFirstRender: false );

;

render()
let input;
const errors = this.state;
console.log(this.props);

const allAccountbyidQueryVars =
id: this.props.id
;

//console.log(allAccountbyidQueryVars);
return (
<Query
query=allAccountbyidQuery
variables=allAccountbyidQueryVars
onCompleted=data => this.setName(data.account.name)
>
( data, loading, error ) =>
<CreateAccountModule account=data.account />;

return (
<Mutation mutation=CREATE_ACCOUNT>
(createAccount, loading, error ) => (
<div>
<form
onSubmit=e =>
e.preventDefault();
const errors = this.validate(
e.target.name.value,
e.target.phone_office.value,
e.target.website.value
);
if (errors)
this.setState( errors );
return;

if (!errors)
let accountres = createAccount(
variables:
name: e.target.name.value,
phone_office: e.target.phone_office.value,
website: e.target.website.value

).then(() => Router.push("/"));
input.value = "";


>
errors && <p>errors</p>
<input
type="text"
name="name"
id="name"
placeholder="Name"
value=this.state.name
onChange=this.handleChange
/>
<input
name="phone_office"
id="phone_office"
placeholder="Phone Number"
//value=data.account.phone_office
//value="123456"
onChange=this.handleChange
/>
<input
name="website"
id="website"
placeholder="Website"
//value=data.account.website
onChange=this.handleChange
/>
<button type="submit">Add Account</button>
<button type="button">Cancel</button>
</form>
loading && <p>Loading...</p>
error && <p>Error :( Please try again</p>
</div>
)
</Mutation>
);

</Query>
);



export default CreateAccountModule;
`


I have tried with props but get props data in apollo state. anyone please suggest possible solution to fix this issue.










share|improve this question
















I have used same form to create and update account(module). Create is working fine but in update mode I am not able to set form field value using Set State methods. I have used query component on render methods and setstate not working on rendor method.



import Mutation from "react-apollo";
import Query from "react-apollo";
import gql from "graphql-tag";
import React, Component from "react";
import Router from "next/router";
import Joi from "joi-browser";

const CREATE_ACCOUNT = gql`
mutation CreateAccount(
$name: String
$phone_office: String
$website: String
)
createAccount(name: $name, phone_office: $phone_office, website:
$website)
name
phone_office
website


`;



export const allAccountbyidQuery = gql`
query account($id: String)
account(id: $id)
id
name
phone_office
website

;
const schema =
name: Joi.string()
.required()
.error(errors =>
return
message: "Name is required!"
;
),
phone_office: Joi.string()
.required()
.error(errors =>
return
message: "Phone Number is required!"
;
),
website: Joi.string()
.required()
.error(errors =>
return
message: "Website is required!"
;
)
;


Main class component



class CreateAccountModule extends React.Component 
static async getInitialProps( query )
const id = query;
return id ;

constructor(props)
super();
this.state =
isFirstRender: true,
name: "",
phone_office: "",
website: ""
;


handleChange = event =>
console.log("hello");
const name, value = event.target;
this.setState( [name]: value );

;
validate(name, phone_office, website)
let errors = "";
const result = Joi.validate(

name: name,
phone_office: phone_office,
website: website
,
schema
);
if (result.error)
errors = result.error.details[0].message;

return errors;

setName = name =>
if (this.state.isFirstRender)
this.setState( name, isFirstRender: false );

;

render()
let input;
const errors = this.state;
console.log(this.props);

const allAccountbyidQueryVars =
id: this.props.id
;

//console.log(allAccountbyidQueryVars);
return (
<Query
query=allAccountbyidQuery
variables=allAccountbyidQueryVars
onCompleted=data => this.setName(data.account.name)
>
( data, loading, error ) =>
<CreateAccountModule account=data.account />;

return (
<Mutation mutation=CREATE_ACCOUNT>
(createAccount, loading, error ) => (
<div>
<form
onSubmit=e =>
e.preventDefault();
const errors = this.validate(
e.target.name.value,
e.target.phone_office.value,
e.target.website.value
);
if (errors)
this.setState( errors );
return;

if (!errors)
let accountres = createAccount(
variables:
name: e.target.name.value,
phone_office: e.target.phone_office.value,
website: e.target.website.value

).then(() => Router.push("/"));
input.value = "";


>
errors && <p>errors</p>
<input
type="text"
name="name"
id="name"
placeholder="Name"
value=this.state.name
onChange=this.handleChange
/>
<input
name="phone_office"
id="phone_office"
placeholder="Phone Number"
//value=data.account.phone_office
//value="123456"
onChange=this.handleChange
/>
<input
name="website"
id="website"
placeholder="Website"
//value=data.account.website
onChange=this.handleChange
/>
<button type="submit">Add Account</button>
<button type="button">Cancel</button>
</form>
loading && <p>Loading...</p>
error && <p>Error :( Please try again</p>
</div>
)
</Mutation>
);

</Query>
);



export default CreateAccountModule;
`


I have tried with props but get props data in apollo state. anyone please suggest possible solution to fix this issue.







reactjs graphql react-apollo next.js






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 22 at 12:03









Abi Sharma

124




124










asked Mar 22 at 11:53









RigalRigal

7811




7811












  • Asked and answered: Setting state in the Query component of react-apollo

    – Daniel Rearden
    Mar 22 at 12:05












  • Thanks i have already check this answer before i asked but How to Define QUERY component outside main class? Thanks

    – Rigal
    Mar 22 at 13:04











  • Like the answer shows, you need to wrap your Mutation component in another component. You cannot maintain the form state inside CreateAccountModule if you're going to initialize it using the query result. Alternatively, move your Query component outside of CreateAccountModule.

    – Daniel Rearden
    Mar 22 at 13:09

















  • Asked and answered: Setting state in the Query component of react-apollo

    – Daniel Rearden
    Mar 22 at 12:05












  • Thanks i have already check this answer before i asked but How to Define QUERY component outside main class? Thanks

    – Rigal
    Mar 22 at 13:04











  • Like the answer shows, you need to wrap your Mutation component in another component. You cannot maintain the form state inside CreateAccountModule if you're going to initialize it using the query result. Alternatively, move your Query component outside of CreateAccountModule.

    – Daniel Rearden
    Mar 22 at 13:09
















Asked and answered: Setting state in the Query component of react-apollo

– Daniel Rearden
Mar 22 at 12:05






Asked and answered: Setting state in the Query component of react-apollo

– Daniel Rearden
Mar 22 at 12:05














Thanks i have already check this answer before i asked but How to Define QUERY component outside main class? Thanks

– Rigal
Mar 22 at 13:04





Thanks i have already check this answer before i asked but How to Define QUERY component outside main class? Thanks

– Rigal
Mar 22 at 13:04













Like the answer shows, you need to wrap your Mutation component in another component. You cannot maintain the form state inside CreateAccountModule if you're going to initialize it using the query result. Alternatively, move your Query component outside of CreateAccountModule.

– Daniel Rearden
Mar 22 at 13:09





Like the answer shows, you need to wrap your Mutation component in another component. You cannot maintain the form state inside CreateAccountModule if you're going to initialize it using the query result. Alternatively, move your Query component outside of CreateAccountModule.

– Daniel Rearden
Mar 22 at 13:09












0






active

oldest

votes












Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55299053%2fset-state-using-query-component-react-apollo%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55299053%2fset-state-using-query-component-react-apollo%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript