1

I have the following 2D array timeArray in the state of my parent component in React. I am using it in the render() function as per follows:

render() {
  return (
    <div><Manager data={this.state.timeArray} /></div>
  );
}

In my Manager component's render() function, I then use the following:

render() {
  return (
    <div>
      {this.props.data.map((weekId, i) => (
        <Week key={i} weekData={weekId} />
      ))}
    </div>
  );
}

I am expecting weekData when I pass it into Week to be an array, as timeArray from my parent component is a 2D array. However, when I use console.log(this.props.weekData) in Week's render() function, I get undefined, not the actual data from the array. What is the best way to pass this in?

EDIT: Per request, here's most of the source:

App

// preceded by various imports
class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      timeArray = []
    }
  }
  handleNewClick() {
    let tempWeek = this.getInput();
    let tempArr = this.state.timeArray.slice(0);
    console.log(tempArr); // logs []
    tempArr.push(tempWeek);
    console.log(tempArr); // logs [1, 1, 1, 1]
    this.setState({ timeArray: tempArr });
  }
  getInput() { // unimplemented, returning expected values for testing
    return [1, 1, 1, 1]
  }
  render() {
    return (
      <div>
        <Manager weeksData={this.state.timeArray} />
        <button onClick={this.handleNewClick.bind(this)}>
          Add Week
        </button
      </div>
    );
  }
}
export default App;

Manager

//various imports
class Manager extends React.Component {
  render() {
    console.log(this.props.weeksData);
    // ^^ logs [1,1,1,1] for each button press, e.g.
    // 1 button press: [[1,1,1,1]]
    // 2 button presses: [[1,1,1,1],[1,1,1,1]] and so on
    return (
      <div>
        {this.props.weeksData.map((weekId, i) => (
          <Week key={i} data={weekId} />
        ))}
      </div>
    );
  }
}
export default Manager;

Week

// various imports
class Week extends React.Components {
  render() {
    let weekData = this.props.weekId;
    console.log(weekData); // logs undefined, which is not as expected
    render() { // unimplemented
      return <div></div>;
    }
  }
}
export default Week;

All code is used and rendered in a Container component, which is:

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';

class Container extends React.Components {
  render() {
    return (
      <div>
        <App />
      </div>
    );
  }
}

ReactDOM.render(
  <Container />,
  document.getElementById('react')
);
4
  • Sorry for the rudimentary question, I'm still learning how to use ReactJS Commented Jun 27, 2017 at 1:11
  • I could not reproduce your problem. Can you post the rest of your code? Commented Jun 27, 2017 at 4:05
  • Can you add the code which initializes the timeArray? Commented Jun 27, 2017 at 4:20
  • @D-reaper done, see most recent edit Commented Jun 27, 2017 at 4:29

2 Answers 2

1

Firstly, you have a lot of typos in your code, I've fixed them in my solution below.

So the main issue here is that you are trying to access props that does not exist. When you pass data to your Week component (data={weekId}), the props data will exist in the Week component, and NOT weekId property. So if you access this.props.data instead of this.props.weekId you will get your data.

// preceded by various imports
class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      timeArray : []
    };
  }
  handleNewClick() {
    let tempWeek = this.getInput();
    let tempArr = this.state.timeArray.slice(0);
    console.log(tempArr); // logs []
    tempArr.push(tempWeek);
    console.log(tempArr); // logs [1, 1, 1, 1]
    this.setState({ timeArray: tempArr });
  }
  getInput() { // unimplemented, returning expected values for testing
    return [1, 1, 1, 1]
  }
  render() {
    return (
      <div>
        <Manager weeksData={this.state.timeArray} />
        <button onClick={this.handleNewClick.bind(this)}>
          Add Week
        </button>
      </div>
    );
  }
}

// various imports
class Manager extends React.Component {
  render() {
    return (
      <div>
        {this.props.weeksData.map((weekId, i) => (
          <Week key={i} data={weekId} />
        ))}
      </div>
    );
  }
}

// various imports
class Week extends React.Component {
  render() {
    let weekData = this.props.weekId;
    console.log(weekData); // logs undefined, which is not as expected
    console.log(this.props.data); // logs the data as expected
    return <div>unimplemented component</div>;
    
  }
}

ReactDOM.render(<App />, document.getElementById('app'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="app"></div>

Sign up to request clarification or add additional context in comments.

1 Comment

Wow, that was a really simple mistake, thanks for the assistance. And my apologies for being a novice, didn't mean to turn this into an extend debugging session.
1

Somewhere in the code you haven't posted there's an error in how you're configuring your timeArray variable. This works fine for me.

class ManagerContainer extends React.Component {
  constructor(props) {
    super(props);
    this.state = { timeArray: [
    {id: 1}, {id: 2}, {id: 3}
    ]}
  }
  render() {
    return (
      <div><Manager data={this.state.timeArray} /></div>
    );
  }
}

class Manager extends React.Component {
  constructor(props) {
    super(props);
  }

  render() {
    return (
      <div>
        {this.props.data.map((week, i) => (
          <Week key={i} weekData={week} />
        ))}
      </div>
    );
  }
}


class Week extends React.Component {
  render() {
    return (
      <p>week number: {this.props.weekData.id}</p>
    )
  }
}
ReactDOM.render(<ManagerContainer />, document.getElementById('app'))

and on jsbin

3 Comments

Does each element of timeArray need an id? The initialization is timeArray: []; in my constructor, but I then push arrays onto it using let temp = [0,1,0], let arr = this.state.timeArray.slice(0) arr.push(temp), and then this.setState({ timeArray: arr }), so timeArray is an array of arrays, but each element doesn't have an id. I can't reference them as this.props.weekData.id in the Week component.
I can add to my question if this isn't clear, as I didn't think this affected the error
The particulars about it shouldn't be important so long as you're handling the data correctly. The code I posted runs like what I think you're expecting. Compare your code to that and start changing it until something makes sense to you.

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.