- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 React Hooks 的新手,正在尝试构建功能组件。在我的组件中,我有两个日历,用户可以选择日期范围。当用户选择日期时,我需要更新我的查询。我尝试使用 useState 但我的状态值总是被盗并且总是给我日历的先前值。我试过 useRef 没有任何成功。
import React, {useState, useEffect, useRef} from 'react';
import { makeStyles } from '@material-ui/core/styles';
import { createRefetchContainer, graphql } from "react-relay";
import MaterialTable, {MTableToolbar} from 'material-table';
import moment from 'moment';
import {
Radio,
RadioGroup,
FormControlLabel } from '@material-ui/core';
import MomentUtils from '@date-io/moment';
import { DatePicker,MuiPickersUtilsProvider } from '@material-ui/pickers';
const useStyles = makeStyles(theme => ({
root: {
flexGrow: 1,
display: 'flex',
flexWrap: 'wrap',
},
control: {
padding: theme.spacing(2),
},
group: {
margin: theme.spacing(0, 1),
},
}));
let liveDate = (numberofDays) => {
return {
Logical_Operator: "AND",
Rules: [
{Field:"startdate", Value: moment().format("MM/DD/YYYY"),
Comparison_Operator: "GREATER_THAN_EQUAL", Type: "String"},
{Field:"startdate", Value:moment().add(numberofDays,'days').format("MM/DD/YYYY"), Comparison_Operator:"LESS_THAN_EQUAL", Type: "String"},
{Field:"active", Value:"True",Comparison_Operator: "EQUAL", Type: "Boolean"}
]
}
}
const UpcomingCampaigns = (props) => {
const [sort] = useState({field: "startdate", direction:"Asc"});
const [value, setValue] = useState("0");
const [startDate, setstartDate] = useState(moment().format("MM/DD/YYYY"));
const [endDate, setendDate] = useState(moment().format("MM/DD/YYYY"));
//https://overreacted.io/how-are-function-components-different-from-classes/
const lateststartDate = useRef(startDate);
const latestendDate = useRef(endDate);
function handleChange(event) {
setValue(event.target.value);
props.relay.refetch({
dynamic_query: liveDate(event.target.value),
sort}, null,
() => {console.log('Refetch done')},
{force:true});
}
useEffect( () => {
lateststartDate.current = startDate;
latestendDate.current = endDate;
props.relay.refetch({
dynamic_query: liveDate(0),
sort});
}, []);
let ReRunQuery = () => {
console.log('StartDate is ', moment(startDate).format("MM/DD/YYYY"))
console.log('EndDate is ', moment(endDate).format("MM/DD/YYYY"))
let newQuery = {
Logical_Operator: "AND",
Rules: [
{Field:"startdate", Value: startDate,
Comparison_Operator: "GREATER_THAN_EQUAL", Type: "String"},
{Field:"enddate", Value:endDate, Comparison_Operator:"LESS_THAN_EQUAL", Type: "String"},
{Field:"active", Value:"True",Comparison_Operator: "EQUAL", Type: "Boolean"}
]
};
console.log('New Query is ', newQuery)
props.relay.refetch({
dynamic_query: newQuery,
sort}, null,
() => {console.log('Refetch done')},
{force:true});
}
const handleStartDateChange = (date) => {
setstartDate(moment(date).format("MM/DD/YYYY"));
console.log('StartDate is ', {startDate})
console.log('latest StartDate is ', moment(lateststartDate.current).format("MM/DD/YYYY"))
//ReRunQuery();
}
const handleEndDateChange = (date) => {
setendDate(moment(date).format("MM/DD/YYYY"));
console.log('Again EndDate is ', moment(latestendDate.current).format("MM/DD/YYYY"))
//ReRunQuery();
}
const buildData = () => {
let tabledata = [];
{props.Viewer &&
props.Viewer.CampaignGrids &&
props.Viewer.CampaignGrids.edges.length &&
props.Viewer.CampaignGrids.edges.map(({ node }) => {
const flattenCampaign = ({Campaign_TypeFull,...rest}) => Object.assign({}, rest, Campaign_TypeFull);
tabledata.push(flattenCampaign(node))
})
}
return tabledata;
}
const renderTable = () => {
let campaignData = buildData();
const classes = useStyles();
return (
<div>
<MaterialTable
columns={[
{ title: 'Name', field: 'Name'},
{ title: 'Campaign Type', field: 'text'},
{ title: 'Start Date', field: 'Start_Date'},
{ title: 'End Date', field: 'End_Date'},
]}
data={campaignData}
title="Upcoming Active Campaigns"
options={
{ paging:false,
search: false,
//totalCount: props.Viewer.Campaigns.totalCount,
backgroundColor: '#EEE',
headerStyle: { backgroundColor: '#039be5', color: '#FFF', 'fontWeight': 'bold', 'fontSize' : '14px'},
rowStyle: {backgroundColor: '#EEE'},
doubleHorizontalScroll: true,
maxBodyHeight:225,
paginationType: 'normal',
emptyRowsWhenPaging: false,
}}
components={{
Toolbar: props => (
<div>
<MTableToolbar {...props} />
<RadioGroup
aria-label="Gender"
name="gender1"
className={classes.group}
value={value}
row={true}
onChange={handleChange}
>
<FormControlLabel value="0" control={<Radio />} label="Today" />
{/* <FormControlLabel value="3" control={<Radio />} label="Next 3 days" />
<FormControlLabel value="7" control={<Radio />} label="Next 7 days" /> */}
<DatePicker value={startDate} label="Start Date" onChange={date => handleStartDateChange(date)} autoOk="true" format="MM/DD/YYYY" variant="inline"
inputVariant="outlined" />
<DatePicker value={endDate} label="End Date" onChange={ date => handleEndDateChange(date)} autoOk="true" format="MM/DD/YYYY" variant="inline"
inputVariant="outlined" />
</RadioGroup>
</div>
)
}}
/>
</div>
)
}
return (
<MuiPickersUtilsProvider utils={MomentUtils}>
{renderTable()}
</MuiPickersUtilsProvider>
)
}
export default createRefetchContainer(
UpcomingCampaigns,
{
Viewer: graphql`
fragment UpcomingCampaigns_Viewer on Viewer @argumentDefinitions(
sort: {type: "Sort", defaultValue:{field: "startdate", direction:"Asc"}}
dynamic_query: {
type: "QueryBuilder",
defaultValue: {
Logical_Operator:AND,
Rules: [
{Field:"startdate", Value: "2019-04-28 23:59:59.000Z",Comparison_Operator:GREATER_THAN_EQUAL, Type: "String"},
{Field:"startdate", Value:"2019-04-28 23:59:59.000Z",Comparison_Operator:LESS_THAN_EQUAL, Type: "String"},
{Field:"active", Value:"True",Comparison_Operator:EQUAL, Type: "Boolean"}
]
}
}
) {
CampaignGrids(sort: $sort, dynamic_query:$dynamic_query ) {
totalCount
edges {
node {
id
Name
Campaign_Type
Start_Date
End_Date
Campaign_TypeFull {
text
value
}
}
}
}
}
`
},
graphql`
query UpcomingCampaignsRefetchQuery(
$dynamic_query: QueryBuilder!
$sort: Sort
) {
Viewer {
...UpcomingCampaigns_Viewer
@arguments(
dynamic_query: $dynamic_query
sort: $sort
)
}
}
`
);
最佳答案
useState 是异步的,因此有时该值不会立即生效 - 您需要实现 useEffect 以使其正常运行
useEffect(() => {
// do something when startDate updates
}, [startDate]);
关于reactjs - React useState 总是给出以前的状态值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57291245/
我是 firebase 的新手,我正在尝试分页查询。我喜欢有一个“下一个”和“上一个”按钮。我的下一个按钮工作正常,我的问题是单击上一个 引用:https://firebase.google.com/
抱歉,标题这么蹩脚,但我只是不知道该放什么,希望你能理解。另外,我不知道以前是否有人问过类似的问题,因为我不知道合适的关键字 - 因此也无法用谷歌搜索。 基本上...在查看preg_match_all
我想在 TFS 中 check out 一个检入文件的先前版本。我可以轻松获得特定文件的变更集 ID 列表,但无法弄清楚如何 checkout 以前的版本。 我目前的代码: var workspace
我想使用 @FunctionalInterface来 self 代码中的 Java 8,但我希望能够将生成的类文件与 Java 6 一起使用。我认为我应该将源版本设为 1.8 , 目标版本为 1.6
自从 versions 被删除以来,我一直无法找到安装以前版本软件的方法。命令并点击 Homebrew。我在 2008 Mac Pro (3,1) 上运行 macOS 10.14.3 (Mojave)
当我开始当前的项目时,App Store 中已经有一个应用程序。此应用程序仅适用于 iPhone。 我的第一个任务是测试和构建一个也可以在 iPod Touch 上运行的版本。 大约 3 周前,App
我在 GitHub 上有一个曾经是 fork 的 repo,但现在不是了,因为我已经删除了原始项目的任何痕迹并开始了一个同名的新项目。 但是,GitHub 仍然表示该项目是 fork 的。有什么方法可
我是一名优秀的程序员,十分优秀!