in Education by
I have the following child component: class SignIn extends React.Component { constructor(props) { super(props); this.onClick = this.handleClick.bind(this); this.state = { email: '', password: '' }; } handleClick = () => { this.props.onClick(this.state.email, this.state.password); } handleEmailChange = (e) => { this.setState({email: e.target.value}); } handlePasswordChange = (e) => { this.setState({password: e.target.value}); } render() { return ( ... ... ); } } Now from the parent I have the following component: class App extends Component { constructor(props) { super(props); this.handleClick = this.handleClick.bind(this); this.state = { email: "", password: "" } } handleClick(e, p, request) { request(); } render() { const { email, password } = this.state; console.log('render', email, password); // here I see the right state after click return ( {}}> {(request, result) => { const { data, loading, error, called } = result; if(!called) { return this.handleClick(e, p, request)} />; } if(error) { return
Error
; } if(loading) { return
Loading...
; } ... return
Mutation processed
; }} ); } } What I wanted to achieve is separate handler after button click and initiate mutation send after some logic. However, this way variables(email, password) are always sent empty to the network. If I put request directly into handle, then it works. How can I have a handler outside of render function to initiate mutation request with correct variable values? I would also very much like to know why this construction doesn't work and variables are empty. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
I think the problem here lies with the line: this.onClick = this.handleClick.bind(this); With incorrect binding you're not going to trigger the method you want. This should be: this.handleClick = this.handleClick.bind(this); The following achieves what you want. I've slimmed it down because I don't know anything about your Apollo implementation, but hopefully you'll get the gist: // SignIn.jsx import React, { Component } from "react"; class SignIn extends Component { constructor(props) { super(props); this.handleClick = this.handleClick.bind(this); this.state = { email: "", password: "" }; } handleClick = () => { this.props.onClick(this.state.email, this.state.password); }; handleEmailChange = e => { this.setState({ email: e.target.value }); }; handlePasswordChange = e => { this.setState({ password: e.target.value }); }; render() { return (
); } } export default SignIn; // App.jsx import React, { Component } from "react"; import ReactDOM from "react-dom"; import SignIn from "./SignIn"; class App extends Component { constructor(props) { super(props); this.handleClick = this.handleClick.bind(this); this.state = { email: "", password: "" }; } handleClick(e, p, request) { console.log("Requesting: ", request); } render() { const { email, password } = this.state; const request = "Your Apollo Request"; console.log("render", email, password); // here I see the right state after click return (
this.handleClick(e, p, request)} />;
); } } export default App;

Related questions

0 votes
    I have the following child component: class SignIn extends React.Component { constructor(props) { super(props ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    When I'm making a request to my backend through a mutation like that: mutation{ resetPasswordByToken(token:" ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 9, 2022 in Education by JackTerrance
0 votes
    I try to learn and implement a simple genetic algorithm library for my project. At this time, evolution, ... Gaussian distribution?.) Select the correct answer from above options...
asked Jan 26, 2022 in Education by JackTerrance
0 votes
    What is django.shortcuts.render function?...
asked Jul 1, 2021 in Technology by JackTerrance
0 votes
    how to pass callback function in javascript?...
asked Dec 10, 2020 in Technology by JackTerrance
0 votes
    How to pass a map from controller to javascript function in VF page?...
asked Dec 10, 2020 in Technology by JackTerrance
0 votes
    I need to render a vector-only SVG without antialiasing in Qt; specifically, I don't want the resulting ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 13, 2022 in Education by JackTerrance
0 votes
    I'm a bit surprised I'm having trouble finding this online, but I can't seem to find an example ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    I'm a bit surprised I'm having trouble finding this online, but I can't seem to find an example ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    I'm a bit surprised I'm having trouble finding this online, but I can't seem to find an example ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    On the left you will notice the google logo rendered by IE, I drew a black line at the top and ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    On the left you will notice the google logo rendered by IE, I drew a black line at the top and ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    On the left you will notice the google logo rendered by IE, I drew a black line at the top and ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 10, 2022 in Education by JackTerrance
0 votes
    I have a Composite Component that uses ajax to change multiple elements when an item is selected. I am ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 18, 2022 in Education by JackTerrance
0 votes
    How is the render time calculated? (a) Date.Now() (b) Date.Now() - performance.timing.domLoading ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 23, 2021 in Education by JackTerrance
...