所以我有一个模块可以呈现当月的日历。它看起来像这样。
let days = {0: 'Sun', 1: 'Mon', 2: 'Tue', 3: 'Wed', 4: 'Thu', 5: 'Fri', 6: 'Sat'};
let today = new Date();
today.setDate(1);
let dayOfWeek = today.getDay();
let count = 0;
class Calender extends Component {
constructor(props) {
super(props);
this.calRender = this.calRender.bind(this);
}
calRender(x) {Object.keys(days).map(index => {count++;
return <td key={x+index} style={{...styleVars.defaultTextColor, ...styleVars.blockSize, backgroundColor: '#777'}}>{count}</td>})}
render() {
return(
<table>
<tr>
{Object.keys(days).map((index) => <th key={index} style={{...styleVars.defaultTextColor, ...styleVars.blockSize, backgroundColor: '#232'}}>{days[index]}</th>)}
</tr>
<tr>
{Object.keys(days).map(index => {
if(index < dayOfWeek) {
return <td key={index} style={{...styleVars.defaultTextColor, ...styleVars.blockSize, backgroundColor: '#bbb'}}></td>}
else {count++;
return <td key={index} style={{...styleVars.defaultTextColor, ...styleVars.blockSize, backgroundColor: '#777'}}>{count}</td>}
})}
</tr>
<tr>
{this.calRender(7)}
</tr>
<tr>
{this.calRender(14)}
</tr>
<tr>
{this.calRender(21)}
</tr>
<tr>
{this.calRender(21)}
</tr>
</table>
)
}
}
问题是calRender函数没有返回任何内容,即使它应该返回带有日期的标记。当我在没有函数的情况下这样做时(通过为每个标签编写map语句),它工作正常。请建议我搞砸了。
答案 0 :(得分:2)
您的 calRender 方法不会返回任何内容。你在map函数的回调中有一个 return 语句,但这还不够。
通过添加return语句来修复它:
calRender(x) {
return Object.keys(days).map(index => {
count++;
return <td key={x+index} style={{...styleVars.defaultTextColor, ...styleVars.blockSize, backgroundColor: '#777'}}>{count}</td>};
);
}
答案 1 :(得分:1)
您的calRender方法不会返回任何内容。
像这样编辑calRender方法:
calRender(x) {
return Object.keys(days).map(index => {
count++;
return (
<td key={x+index} style={{...styleVars.defaultTextColor, ...styleVars.blockSize, backgroundColor: '#777'}}>
{count}
</td>
)
})
}