我在父组件中创建了一个 setter 函数并将其传递给它的子组件:
有问题的部分:
export default function Day({ dayInfo, props }) {
var [timeOfDay, setTimeOfDay] = useState('');
function TimeOfDaySetter(index) {
console.log('index ', index);
if (index === 0) {
setTimeOfDay((timeOfDay) => (timeOfDay = 'AM'));
return <Header as="h1">{timeOfDay}</Header>;
} else if (index === 12) {
setTimeOfDay((timeOfDay) => (timeOfDay = 'PM'));
return <Header as="h1">{timeOfDay}</Header>;
}
}
该函数嵌套在子函数的 map 函数中:
{Array.from(Array(amountOfRows)).map((row, index) => {
return (
<React.Fragment key={index}>
<Table.Row>
<Table.Cell rowSpan="2" style={tableStyle}>
{TimeOfDaySetter(index)}
</Table.Cell>
但这是跳过第一个条件?
任何人都可以帮助为什么会发生这种情况?
完整的父级和组件:
import { Header, Table, TextArea } from 'semantic-ui-react';
import React, { useState, useEffect } from 'react';
export default function Day({ dayInfo, props }) {
var [dayInfoInChild, setDayInfoInChild] = useState([]);
var [timeOfDay, setTimeOfDay] = useState('');
function setExactHourHelper(index) {
return index === 0 ? 12 : '' || index > 12 ? index - 12 : index;
}
function TimeOfDaySetter(index) {
console.log('index ', index);
if (index === 0) {
setTimeOfDay((timeOfDay) => (timeOfDay = 'AM'));
return <Header as="h1">{timeOfDay}</Header>;
} else if (index === 12) {
setTimeOfDay((timeOfDay) => (timeOfDay = 'PM'));
return <Header as="h1">{timeOfDay}</Header>;
}
}
useEffect(() => {
if (dayInfo !== null) {
var modifiedDayInfo = dayInfo
.split(' ')
.map((item) => {
if (item.indexOf(',')) return item.replace(/,/g, '');
})
.join('-');
if (localStorage.getItem(modifiedDayInfo)) {
// setDayInfoInChild(function (dayInfoInChild) {
// return [...setDayInfoInChild, modifiedDayInfo];
// });
console.log(modifiedDayInfo);
} else {
localStorage.setItem(modifiedDayInfo, JSON.stringify({}));
}
}
}, [dayInfo, timeOfDay, timeOfDay]);
function TableLayout({ TimeOfDaySetter }) {
var [amountOfRows, setAmountOfRows] = useState(24);
var [textValue, setTextValue] = useState('');
function handleChange(event) {
setDayInfoInChild(event.target.value);
}
const tableStyle = {
borderLeft: 0,
borderRight: 0,
};
const colorOveride = {
color: '#C1BDBD',
};
return (
<>
<h1>{dayInfo}</h1>
<Table celled structured>
<Table.Body>
{Array.from(Array(amountOfRows)).map((row, index) => {
return (
<React.Fragment key={index}>
<Table.Row>
<Table.Cell rowSpan="2" style={tableStyle}>
{TimeOfDaySetter(index)}
</Table.Cell>
<Table.Cell style={tableStyle}>
{
<strong>
{setExactHourHelper(index)}
:00
</strong>
}
<TextArea
rows={2}
name="textarea"
value={textValue}
onChange={handleChange}
placeholder="Tell us more"
/>
</Table.Cell>
</Table.Row>
<Table.Row>
<Table.Cell style={(tableStyle, colorOveride)}>
{
<strong>
{setExactHourHelper(index)}
:30
</strong>
}
<TextArea rows={2} placeholder="Tell us more" />
</Table.Cell>
</Table.Row>
</React.Fragment>
);
})}
</Table.Body>
</Table>
</>
);
}
{
if (dayInfo === null) {
return <p>Loading...</p>;
}
}
return (
<React.Fragment>
<TableLayout
dayInfo={dayInfo}
timeOfDay={timeOfDay}
TimeOfDaySetter={TimeOfDaySetter}
/>
</React.Fragment>
);
}