我在从主渲染类中的处理程序中有条件地调用一个突变时遇到麻烦。不幸的是,我无法实现一个提交按钮(项目定义的局限性,我的DOM输入必须动态地渲染onChange的新图),并且必须在允许执行变异之前验证条件,但是我似乎无法诊断出针对该问题的修复程序这个!
下面,您可以找到父组件代码。请注意,该变异仍在验证处理程序内,暂时停在那里直到建立修复程序为止。
我也有一些阿波罗文档教程指出了我的方法,但是不幸的是,它们依赖于由于项目限制而无法复制的不同项目结构。
下面,您可以找到父组件代码。请注意,该变异仍在验证处理程序内,暂时停在那里直到建立修复程序为止。
import React, { Component } from "react";
import CurrencyInput from "./CurrencyInput";
import SliderInput from "./SliderInput";
import DisplayGraph from "./DisplayGraph";
import "./InputGraphSection.css";
import FrequencyInput from "./FrequencyInput";
import { Mutation } from "react-apollo";
import gql from "graphql-tag";
const SAVINGS_MUTATION = gql`
mutation savingsmutation(
$paymentFrequency: Int!
$initialDeposit: Float!
$monthlyDeposit: Float!
$interestRate: Float!
) {
createSavings(
paymentFrequency: $paymentFrequency
initialDeposit: $initialDeposit
monthlyDeposit: $monthlyDeposit
interestRate: $interestRate
) {
savings {
months {
id
totalInterest
totalValue
}
}
}
}
`;
export default class InputGraphSectionContainer extends Component {
constructor(props) {
super(props);
this.state = {
savT: [{ x: 0, y: 0 }],
intT: [{ x: 0, y: 0 }]
};
}
handleComplete = ({ data: { createSavings } }) => {
this.setState(prevState => ({
savT: [
...prevState.savT,
// month is inside the data returned by the API????
{ x: createSavings.savings.months.id, y: createSavings.savings.months.totalValue }
],
intT: [
...prevState.intT,
{ x: createSavings.savings.months.id, y: createSavings.savings.months.totalInterest }
]
}));
};
render() {
const { savT, intT } = this.state;
return (
<Mutation mutation={SAVINGS_MUTATION} onCompleted={this.handleComplete}>
{savingsmutation => (
<InputGraphSection mutate={savingsmutation} savT={savT} intT={intT} />
)}
</Mutation>
);
}
}
class InputGraphSection extends Component {
constructor(props) {
super(props);
this.state = {
initialDeposit: "",
monthlyDeposit: "",
interestRate: 0,
paymentFrequency: ""
};
}
componentDidUpdate({ mutate }, prevState) {
console.log(this.state);
if (
this.state.initialDeposit !== "" &&
this.state.monthlyDeposit !== "" &&
this.state.paymentFrequency !== "" &&
prevState !== this.state
) {
//If currencyInput elements are returning strings, convert to ints here.
var paymentF = Number(this.state.paymentFrequency);
var initialD = parseFloat(this.state.initialDeposit);
var monthlyD = parseFloat(this.state.monthlyDeposit);
var interestR = parseFloat(this.state.interestRate)/100;
console.log("execute mutation");
mutate({
variables: {
paymentFrequency: paymentF,
initialDeposit: initialD,
monthlyDeposit: monthlyD,
interestRate: interestR
}
});
console.log("Mutation query commencing")
} else {
console.log("Input Requirements not met, will not generate graph.");
}
}
handleChange = evt => {
const { name, value } = evt.target;
this.setState({ [name]: value });
};
render() {
const {
initialDeposit,
monthlyDeposit,
interestRate,
paymentFrequency
} = this.state;
const { savT, intT } = this.props;
return (
<div>
<p className="input-label">
Inputs must be positive and have no more than 15 digits with 2 decimal
places!
</p>
<div className="financial-inputs">
<p className="input-label">What is your initial Deposit?</p>
<CurrencyInput
name="initialDeposit"
value={initialDeposit}
onInputChange={this.handleChange}
/>
<p className="input-label">How much will you save each month?</p>
<CurrencyInput
name="monthlyDeposit"
value={monthlyDeposit}
onInputChange={this.handleChange}
/>
<p className="input-label">
What is the annual interest rate you have acquired?
</p>
<SliderInput
name="interestRate"
value={Number(interestRate)}
onInputChange={this.handleChange}
/>
<p className="input-label">
Specify the frequency of interest compounding.
</p>
<FrequencyInput
name="paymentFrequency"
value={paymentFrequency}
onInputChange={this.handleChange}
/>
</div>
<div className="financial-display">
<DisplayGraph savT={savT} intT={intT} />
</div>
</div>
);
}
}
答案 0 :(得分:0)
您可以有多种方式有条件地调用阿波罗突变,因为通常有多种方式可以调用突变。这些方式包括Mutation
组件,直接在客户端上调用mutate或使用graphql
HOC。
您的示例使用的是Mutation
component,它遵循渲染道具模式。为了使用它,您需要渲染组件,然后调用它提供的突变:
...
render() {
return (
<Mutation
mutation={SAVINGS_MUTATION}
variables={{
paymentFrequency: paymentF,
initialDeposit: initialD,
monthlyDeposit: monthlyD,
interestRate: interestR
}}
>
{(savingsmutation, { data }) => {
return (
<CurrencyInput
value={initialDeposit}
onInputChange={() => savingsmutation()}
/>
)
}}
</Mutation>
)
}
...
您还可以使用withApollo
HOC直接访问客户端并在其上调用mutate
。
import { withApollo } from 'react-apollo'
class InputGraphSection extends Component {
handleChange() {
this.props.client.mutate({
mutation: SAVINGS_MUTATION,
variables: {
paymentFrequency: paymentF,
initialDeposit: initialD,
monthlyDeposit: monthlyD,
interestRate: interestR
}
})
}
}
export default withApollo(InputGraphSection)
最后使用graphql
HOC
import { graphql } from 'react-apollo'
class InputGraphSection extends Component {
handleChange() {
this.props.mutate({
paymentFrequency: paymentF,
initialDeposit: initialD,
monthlyDeposit: monthlyD,
interestRate: interestR
})
}
}
export default graphql(SAVINGS_MUTATION)(InputGraphSection)