Giter Club home page Giter Club logo

Comments (6)

buzbohdan avatar buzbohdan commented on May 27, 2024 3

@spacesuitdiver I'm importing svg files using require, and it only works if I use default attribute of the object returned by required call.

So I think what you need is <ListItem icon={require('../path/to/yo-that.svg').default} />

from react-native-svg-transformer.

spacesuitdiver avatar spacesuitdiver commented on May 27, 2024

It doesn't seem like require() syntax works at all 🤷‍♂

from react-native-svg-transformer.

spacesuitdiver avatar spacesuitdiver commented on May 27, 2024

Closest I could get is declaring all the imports up top import MySvg1 from '...'; etc.

from react-native-svg-transformer.

kristerkari avatar kristerkari commented on May 27, 2024

Thanks @spacesuitdiver! It looks indeed like the require syntax is not supported at all. I have always been using import, so I have not even noticed it.

I wonder if there is something in the Metro packager configuration that is related to this? https://facebook.github.io/metro/docs/en/configuration

from react-native-svg-transformer.

geraintwhite avatar geraintwhite commented on May 27, 2024

@buzbohdan any idea how to get that working with flow and jest?

Flow throws the following error:

Cannot get `require(...).default` because property `default` is missing in  `String` [1].Flow(InferError)

And Jest sees the SVG component as undefined using the supplied svgMock.js.

I managed to get require(...).default working in jest by using the following mock, but then importing the SVG results in { default: 'Svg' }.

exports.default = 'Svg';
exports.default.ReactComponent = 'Svg';

from react-native-svg-transformer.

geraintwhite avatar geraintwhite commented on May 27, 2024

I got it working with the following flow type definition and jest mock.

flow-typed/svg.js

// @flow

declare module SvgImageStub {
  declare export default 'Svg';
}

.flowconfig

module.name_mapper='^[./a-zA-Z0-9$_-]+\.svg$' -> 'SvgImageStub'

__mocks__/svg.js

// @flow

const Svg = 'Svg';

export default Svg;

Now it works with both import ... from '...' and require('...').default.

from react-native-svg-transformer.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.