Receiving an Array to render in component

The_Enigma

I am trying to pass my state of thumbnail URLs that are saved to an array state. But when evaluating in my Netflix component videos is empty? When I console it, it returns In tile {"videos":[]}

Render

return (
  <div className="explore">
    <div className="row">
      <NetflixTile videos={this.state.thumbnail} />
    </div>
  </div>
);

Constructor

 constructor(props) {
        super(props);
        this.props.getVideos();
        this.state = {
          thumbnail: []
        };
      }

DidMount EDITED

componentDidUpdate() {
    console.log("Component updated?");

    let i = 0;
    if (this.props.videos == null) {
      console.log("It's null");
    } else {
      const videos = this.props.videos.video.map(video => {
        <h5 key={video._id}>{video.thumbnail}</h5>;
        this.state.thumbnail[i] = video.thumbnail;
        console.log(this.state.thumbnail);
        i++;
      });
    }
  }

Netflix component added into Render

const NetflixTile = videos => {
  console.log("In tile " + JSON.stringify(videos.videos));
  if (videos.length != null) {
    for (let i = 0; videos > i; i++) {
      return (
        <div className="row__inner">
          <div className="tile">
            <div className="tile__media">
              <img
                className="tile__img"
                id="thumbnail"
                src={videos[i]}
                alt=""
              />
            </div>
          </div>
        </div>
      );
    }
  } else {
    return (
      <div>
        <h1>
          You have not yet uploaded any STEM content. Go to your dashboard page
          and click Upload to add to this library.
        </h1>
      </div>
    );
  }
};

export default NetflixTile;

**Console output of this.state.thumbnail ** enter image description here

amankkg

OK, since you've got more or less working solution, let me update my answer with some suggestions then :)

  1. Asynchronous calls (e.g. fetch) should be performed in componentDidMount (it is true until React's Suspense and async stuff is landed);

  2. If you're not modifying videos' state of the parent component - use props directly, no need to bother with any state here at all.

  3. In another case, keep in mind that componentDidUpdate will be called after each prop change or state change. Previously, I'd suggest using a componentWillReceiveProps for this kind of logic. But with React 16 you can use getDerivedStateFromProps instead. Here is what official docs are suggesting:

    If you want to “reset” some state when a prop changes, consider either making a component fully controlled or fully uncontrolled with a key instead.

    As you can see, it is better to handle such comparison logic (e.g. props.videos !== state.videos) outside of component and make this component fully controlled, so it renders props directly and updates only when new props are received.

  4. Try to minimize setState calls, however, you might not notice a big difference even with your current solution - it is because of React is batching state updates.

  5. In JSX, it is better to use Array.prototype.map and ternary operators instead of for loops and if statements. Prefer working with value transformations rather than imperative statement jumping.

Applying these suggestions it can end up like this:

class VideosParentComponent extends React.Component {
  static getDerivedStateFromProps(props, state) {
    if (props.videos !== state.videos) {
      return { videos: props.videos }
    }

    return null
  }

  state = { videos: [] }

  componentDidMount() {
    this.props.getVideos()
  }

  render() {
    const thumbnails = this.state.videos.video.map(video => video.thumbnail)
    return (
      <div className="explore">
        <div className="row">
          {thumbnails.length !== 0
            ? <NetflixTitle videos={thumbnails} />
            : (
              <div>
                <h1>
                  You have not yet uploaded any STEM content. Go to your dashboard page
                  click Upload to add to this library.
                </h1>
              </div>
            )}
        </div>
      </div>
    )
  }
}

const NetflixTitle = ({ videos }) => (
  <div className="row__inner">
    {videos.map(x => (
      <div key={x} className="tile">
        <div className="tile__media">
          <img
            id={`thumbnail_${x}`}
            src={x}
            className="tile__img"
            alt=""
          />
        </div>
      </div>
    ))}
  </div>
)

Initial answer

You're passing videos as a prop in JSX, not as a function parameter. This is why you should retrieve it as a key in props in a destination component.

const NetflixTitle = ({ videos }) => { // rest of the code

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Map through an array and render a specified component in React

From Dev

Component not receiving new props

From Dev

How to render an object from an array into another component in React?

From Dev

Render a property of an object, within array, inside React Component's state

From Dev

AngularJS - Component receiving dynamic content

From Dev

Why receiving an empty array?

From Dev

Receiving to array of structs

From Dev

Receiving an array in a function

From Dev

How to render a Component if the prop is a component?

From Dev

Slicing an array in redux store always results in removing the last array item on component render

From Dev

React render nested component

From Dev

Ember re render component

From Dev

Render reactjs component not work

From Dev

React component does not render

From Dev

Render React Component to Fancybox

From Dev

Render component outside of Route

From Dev

ReactJS Render Component

From Dev

REACT - Error to render component

From Dev

Render a timer component onClick

From Dev

Render undefined in react component?

From Dev

Conditionally render DataSearch component

From Dev

Can't get values from Array object to render React component [solved]

From Dev

Nested React/Relay component not receiving props

From Dev

React child component receiving props as undefined

From Dev

receiving onPaste / onChange through a component that returns input

From Dev

@Input not receiving data from parent component

From Dev

React Native child component not receiving updated props

From Dev

React Component receiving incorrect prop value

From Dev

Array not receiving radio buttons value

Related Related

HotTag

Archive