0

So I have a module that renders a calender for the current month. It looks like this.

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>

        )
    }
}

The problem is that calRender function is not returning anything, even though it is supposed to return tags with dates. When I am doing this without a function(by writing map statements for each tag), it is working fine. Please suggest where I have messed this up.

2 Answers 2

3

Your calRender method does not return anything. You do have a return statement in the map function's callback, but that's not enough.

Fix it by adding a return statement:

calRender(x) {
    return Object.keys(days).map(index => {
        count++;
        return <td key={x+index} style={{...styleVars.defaultTextColor, ...styleVars.blockSize, backgroundColor: '#777'}}>{count}</td>};
    );
}
Sign up to request clarification or add additional context in comments.

Comments

1

Your calRender method does not return anything.

Edit calRender method like this:

calRender(x) {
   return Object.keys(days).map(index => {
      count++;
      return (
        <td key={x+index} style={{...styleVars.defaultTextColor, ...styleVars.blockSize, backgroundColor: '#777'}}>
          {count}
        </td>
      )
   })
}

2 Comments

I have initialized count at the top with let count = 0 statement.
@Sanchit you should use state for this case. reactjs.org/docs/state-and-lifecycle.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.