Lottie-web: React Lottie Error: <svg> attribute width: Expected length, "undefined".

Created on 9 Jun 2020  路  3Comments  路  Source: airbnb/lottie-web

Hi, I am having a problem with render the animation. Please refer to screenshot for exact error message. I am using react and rails for this project.
What i wanted to do is getting the JSON data from database/api, and pass as props to a component and then rendering it. Appreciate if anyone could help me with this. Thank you. This is my code in my component:

import React, { useEffect, useRef } from "react";
import lottie from "lottie-web";

function Anim(props) {
  const container = useRef(null);
  const animationDataVar = props.code;
  useEffect(() => {
    lottie.loadAnimation({
      container: container.current,
      renderer: "svg",
      loop: true,
      autoplay: true,
      animationData: animationDataVar,
      rendererSettings: { preserveAspectRatio: "xMidYMid slice" },
    });
  }, []);

  return (
    <div>
      <div className="container" ref={container}></div>
    </div>
  );
}

export default Anim;

Screenshot 2020-06-09 at 8 05 31 PM

Most helpful comment

Hi, the prob is solved. I need to JSON parsed it first, ie. const animationDataVar = JSON.parse(props.code);

All 3 comments

hi, seems that the json is incorrect. Do you hav a link with a working example?

Hi, I am sorry my link is not working because of this issue, nothing gets rendered. Is it ok if u take a look at my repo instead? My repo is https://github.com/yannieyeung/SEI-Project4-Design-sharing-App. Thank you!

Hi, the prob is solved. I need to JSON parsed it first, ie. const animationDataVar = JSON.parse(props.code);

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jumostudio picture jumostudio  路  3Comments

ritsraghani picture ritsraghani  路  3Comments

ochanje210 picture ochanje210  路  3Comments

phileastv picture phileastv  路  3Comments

leantide picture leantide  路  3Comments