我在js互操作方面遇到麻烦。我正在尝试像这样使用js组件react-slick:
// src/interop/Carousel.js
import React from 'react'
import Slider from 'react-slick'
export function Carousel(props) {
return (
<Slider>
<div>
<h3>{props.name} 1</h3>
</div>
</Slider>
)
}
/* src/Carousel.re */
[@bs.module "./interop/Carousel"] [@react.component]
external make: (~name: string) => React.element = "";
/* src/index.re */
ReactDOMRe.renderToElementWithId(<Carousel name="ahaha" />, "carousel");
但是在webpack中遇到了这个错误:
ERROR in ./lib/js/src/Index.bs.js
Module not found: Error: Can't resolve './interop/Carousel' in '[...]/reason_react_example/lib/js/src'
@ ./lib/js/src/Index.bs.js 6:15-44
所以看起来BS在编译时不考虑Carousel.js
文件吗?
顺便说一句,我正在关注这个reason-react doc
答案 0 :(得分:2)
默认情况下,BuckleScript会将生成的js工件放入lib/js/...
中,因此您必须相对于其编写导入,或者配置bsb
以便将工件与源文件一起放置。您可以通过为"in-source": true
中的给定package-spec
设置bsconfig.json
来完成后者。例如:
{
"package-specs": {
"module": "commonjs",
"in-source": true
}
}
答案 1 :(得分:1)
经过一些调整后,这对我有用:
// lib/js/src/interop/Carousel.js
import React from 'react'
import Slider from 'react-slick'
const Carousel = props => {
return (
<Slider>
<div>
<h3>{props.name} 1</h3>
</div>
</Slider>
)
}
export default Carousel
// src/Carousel.re
[@bs.module "./interop/Carousel"] [@react.component]
external make: (~name: string) => React.element = "default"; // handle default export
// src/Index.re
ReactDOMRe.renderToElementWithId(<Carousel name="it works!" />, "carousel");
由于Carousel.js
使用的是es6和jsx,因此我需要设置webpack才能使用它(es6,jsx)。并且bsconfig.json
需要具有以下设置:
"reason": {
"react-jsx": 3
},
"package-specs": [
{
"module": "commonjs",
"in-source": false
}
]