React functional component TypeError: Cannot read property 'props' of undefined
React functional component TypeError: Cannot read property 'props' of undefined
几个小时后阅读如何绑定 this 我放弃了。这是我第一次用react,我在Whosebug上查看了20多个相关问题,但我无法解决我的问题。
我在 guidelines.
之后安装了 react-multi-carousel 组件
我只是简单地复制并粘贴了常用代码,但出现了这个恼人的错误:
TypeError: Cannot read property 'props' of undefined
Index
src/views/Index.js:107
104 | responsive={responsive}
105 | ssr={true} // means to render carousel on server-side.
106 | infinite={true}
> 107 | autoPlay={this.props.deviceType !== "mobile" ? true : false}
| ^ 108 | autoPlaySpeed={1000}
109 | keyBoardControl={true}
110 | customTransition="all .5"
我尝试遵循我找到的几乎所有答案(主要是绑定 this),但没有成功。
这是我的代码:
import React from "react";
// reactstrap components
import {
Container
} from "reactstrap";
import Carousel from 'react-multi-carousel';
import 'react-multi-carousel/lib/styles.css';
// core components
import ScrollTransparentNavbar from "components/Navbars/ScrollTransparentNavbar.js";
import IndexHeader from "components/Headers/IndexHeader.js";
const responsive = {
desktop: {
breakpoint: { max: 3000, min: 1024 },
items: 3,
slidesToSlide: 3, // optional, default to 1.
},
tablet: {
breakpoint: { max: 1024, min: 464 },
items: 2,
slidesToSlide: 2, // optional, default to 1.
},
mobile: {
breakpoint: { max: 464, min: 0 },
items: 1,
slidesToSlide: 1, // optional, default to 1.
},
};
function Index() {
React.useEffect(() => {
document.body.classList.add("index-page");
document.body.classList.add("sidebar-collapse");
document.documentElement.classList.remove("nav-open");
window.scrollTo(0, 0);
document.body.scrollTop = 0;
return function cleanup() {
document.body.classList.remove("index-page");
document.body.classList.remove("sidebar-collapse");
};
});
return (
<>
<ScrollTransparentNavbar />
<div className="wrapper">
<IndexHeader />
<Carousel
swipeable={false}
draggable={false}
showDots={true}
responsive={responsive}
ssr={true} // means to render carousel on server-side.
infinite={true}
autoPlay={this.props.deviceType !== "mobile" ? true : false}
autoPlaySpeed={1000}
keyBoardControl={true}
customTransition="all .5"
transitionDuration={500}
containerClass="carousel-container"
removeArrowOnDeviceType={["tablet", "mobile"]}
deviceType={this.props.deviceType}
dotListClass="custom-dot-list-style"
itemClass="carousel-item-padding-40-px">
<div>Item 1</div>
<div>Item 2</div>
<div>Item 3</div>
<div>Item 4</div>
</Carousel>
</div>
</div>
</>
);
}
export default Index;
- 你没有定义
props
- 功能组件中不需要
this
const Index = (props) => { ...
// or
function Index(props) {...
autoPlay={props.deviceType ...
在你的功能组件中,你应该像这样添加道具
function Index(props) {
...
}
函数组件不通过 this.props
获取其 props,而是将它们作为函数的第一个参数对象获取。
function Index (props) {
// ...
return (
// ...
autoPlay={props.deviceType !== mobile}
)
}
几个小时后阅读如何绑定 this 我放弃了。这是我第一次用react,我在Whosebug上查看了20多个相关问题,但我无法解决我的问题。
我在 guidelines.
之后安装了 react-multi-carousel 组件我只是简单地复制并粘贴了常用代码,但出现了这个恼人的错误:
TypeError: Cannot read property 'props' of undefined
Index
src/views/Index.js:107
104 | responsive={responsive}
105 | ssr={true} // means to render carousel on server-side.
106 | infinite={true}
> 107 | autoPlay={this.props.deviceType !== "mobile" ? true : false}
| ^ 108 | autoPlaySpeed={1000}
109 | keyBoardControl={true}
110 | customTransition="all .5"
我尝试遵循我找到的几乎所有答案(主要是绑定 this),但没有成功。
这是我的代码:
import React from "react";
// reactstrap components
import {
Container
} from "reactstrap";
import Carousel from 'react-multi-carousel';
import 'react-multi-carousel/lib/styles.css';
// core components
import ScrollTransparentNavbar from "components/Navbars/ScrollTransparentNavbar.js";
import IndexHeader from "components/Headers/IndexHeader.js";
const responsive = {
desktop: {
breakpoint: { max: 3000, min: 1024 },
items: 3,
slidesToSlide: 3, // optional, default to 1.
},
tablet: {
breakpoint: { max: 1024, min: 464 },
items: 2,
slidesToSlide: 2, // optional, default to 1.
},
mobile: {
breakpoint: { max: 464, min: 0 },
items: 1,
slidesToSlide: 1, // optional, default to 1.
},
};
function Index() {
React.useEffect(() => {
document.body.classList.add("index-page");
document.body.classList.add("sidebar-collapse");
document.documentElement.classList.remove("nav-open");
window.scrollTo(0, 0);
document.body.scrollTop = 0;
return function cleanup() {
document.body.classList.remove("index-page");
document.body.classList.remove("sidebar-collapse");
};
});
return (
<>
<ScrollTransparentNavbar />
<div className="wrapper">
<IndexHeader />
<Carousel
swipeable={false}
draggable={false}
showDots={true}
responsive={responsive}
ssr={true} // means to render carousel on server-side.
infinite={true}
autoPlay={this.props.deviceType !== "mobile" ? true : false}
autoPlaySpeed={1000}
keyBoardControl={true}
customTransition="all .5"
transitionDuration={500}
containerClass="carousel-container"
removeArrowOnDeviceType={["tablet", "mobile"]}
deviceType={this.props.deviceType}
dotListClass="custom-dot-list-style"
itemClass="carousel-item-padding-40-px">
<div>Item 1</div>
<div>Item 2</div>
<div>Item 3</div>
<div>Item 4</div>
</Carousel>
</div>
</div>
</>
);
}
export default Index;
- 你没有定义
props
- 功能组件中不需要
this
const Index = (props) => { ...
// or
function Index(props) {...
autoPlay={props.deviceType ...
在你的功能组件中,你应该像这样添加道具
function Index(props) {
...
}
函数组件不通过 this.props
获取其 props,而是将它们作为函数的第一个参数对象获取。
function Index (props) {
// ...
return (
// ...
autoPlay={props.deviceType !== mobile}
)
}