reactjs - 如何运行突变 onChange,同时链接其他选项?
问题描述
为新手问题道歉,但我想在编辑屏幕上有一个布尔字段并使用 React Switch ( https://github.com/markusenglund/react-switch ) 组件来呈现开关。我不想使用表单提交按钮,而是想在动态更改开关时运行突变。此外,我想切换子选项的可见性并设置它们的禁用状态并根据“主”开关的状态更改它们的值。我的笔记在下面的代码中......
我仍然在这里学习,所以这是我所得到的。我试过谷歌搜索,但似乎找不到答案。
这是我的代码的副本,但我对其进行了一些更改/剥离:
import React, { Component } from "react";
import { Query, Mutation } from "react-apollo";
import gql from "graphql-tag";
import PropTypes from "prop-types";
import Error from "../ErrorMessage";
import Switch from "react-switch";
const MY_ACCOUNT_QUERY = gql`
query {
me {
id
account {
toggleOptionOne
toggleOptionTwo
toggleOptionThree
}
}
}
`;
const UPDATE_ACCOUNT_MUTATION = gql`
mutation UPDATE_ACCOUNT_MUTATION(
$toggleOptionOne: Boolean
$toggleOptionTwo: Boolean
$toggleOptionThree: Boolean
) {
updateAccount(
toggleOptionOne: $toggleOptionOne
toggleOptionTwo: $toggleOptionTwo
toggleOptionThree: $toggleOptionThree
) {
toggleOptionOne
toggleOptionTwo
toggleOptionThree
}
}
`;
class Visibility extends Component {
render() {
return (
<Query query={MY_ACCOUNT_QUERY}>
{({ data: { me }, loading }) => {
if (loading) return <p>Loading...</p>;
if (!me.id) return <p>No user found.</p>;
return (
<>
<VisibilityToggles account={me.account} key={me.account.id} />
</>
);
}}
</Query>
);
}
}
class VisibilityToggles extends React.Component {
static propTypes = {
account: PropTypes.shape({
toggleOptionOne: PropTypes.Boolean,
toggleOptionTwo: PropTypes.Boolean,
toggleOptionThree: PropTypes.Boolean
}).isRequired
};
state = {
toggleOptionOne: this.props.account.toggleOptionOne,
toggleOptionTwo: this.props.account.toggleOptionTwo,
toggleOptionThree: this.props.account.toggleOptionThree
};
handleChange = (checked, event, id) => {
this.setState({ [id]: checked });
};
render() {
return (
<Mutation
mutation={UPDATE_ACCOUNT_MUTATION}
variables={{
toggleOptionOne: this.state.toggleOptionOne,
toggleOptionTwo: this.state.toggleOptionTwo,
toggleOptionThree: this.state.toggleOptionThree
}}
>
{(updateAccount, { loading, error }) => (
<>
{error && <Error error={error} />}
<label htmlFor="toggleOptionOne">
<Switch
id="toggleOptionOne"
name="toggleOptionOne"
onChange={this.handleChange}
checked={this.state.toggleOptionOne} />
Master Option Toggle
</label>
{/* NOTE: I'd like to run the mutation to save the changed value to the database on the fly without having to have a form submit button */}
{/* NOTE: toggling above to ON should REVEAL the following 2 items. */}
{/* Toggling above to OFF should HIDE the following 2 items AND set them to false and disabled. */}
<fieldset id="THIS_BLOCK_SHOULD_TOGGLE">
<div>
<label htmlFor="toggleOptionTwo">
<Switch
id="toggleOptionTwo"
name="toggleOptionTwo"
onChange={this.handleChange}
checked={this.state.toggleOptionTwo} />
Sub Option Toggle 1
</label>
{/* NOTE: I'd like to run the mutation to save the changed value to the database on the fly without having to have a form submit button */}
</div>
<div>
<label htmlFor="toggleOptionThree">
<Switch
id="toggleOptionThree"
name="toggleOptionThree"
onChange={this.handleChange}
checked={this.state.toggleOptionThree} />
Sub Option Toggle 2
</label>
{/* NOTE: I'd like to run the mutation to save the changed value to the database on the fly without having to have a form submit button */}
</div>
</fieldset>
</>
)}
</Mutation>
);
}
}
export default Visibility;
TLDR:我想做 2 件事:1. 更改开关时运行突变 2. 更改主开关时,显示/隐藏子选项并启用/禁用它们并将其值设置为 false如果主开关为假。
再次道歉,如果这是一个新手问题。我还在学习:)提前谢谢!
解决方案
我认为有几种方法可以实现您想要的。有些可能需要进行一些重组。就像现在一样,我可以建议:
1) 将 updateAccount 作为参数传递给 handleChange 函数,并在您认为合适的时候执行它。2) 签出 Mutation 组件的 refetch 属性。
refetchQueries
: (mutationResult: FetchResult) => Array<{ query: DocumentNode, variables?: TVariables} | 字符串>
推荐阅读
- java - 无法使用 lambda 表达式创建 ThreadLocal 类
- django - 对两个单独表(Django、postgres)上的列应用唯一约束
- visual-studio - Azure Web 作业运行命令设置不正确
- c# - 在不重启服务器的情况下重新加载 asp.net core swagger ui
- applescript - 为什么将文件夹放到 Applescript 应用程序上会显示一个对话框?
- c# - C#如何控制自定义类的序列化过程?
- php - 如何在 voyager 项目的编辑视图中包含选择输入?
- c# - 通过 HttpClient 使用 Google Cloud AutoML 时收到 401“未授权”错误
- xpath - 如何在 Xpath 中使用跟随来获取标签中的兄弟姐妹
- tensorflow - 了解COCO评估“最大检测数”