이미지를 가져 오기 위해 PHP에 API가 있으며 다음 반응 회전 목마 에 표시하고 싶습니다 . 내가 어디에서 잘못하고 있는지 모르겠지만 이미지가 캐 러셀에 표시되지 않습니다. 지난 3 일 동안이 일을 시도했지만 아무것도 표시하지 않습니다. 누군가 제발 도와주세요.
import React from "react";
import {Link} from "react-router-dom";
import "./cropSelection.css";
import Carousel, { Dots } from '@brainhubeu/react-carousel';
import '@brainhubeu/react-carousel/lib/style.css';
import 'react-image-picker/dist/index.css'
import axios from 'axios';
class CropSelection extends React.Component {
handleChange = selectedOption => {
this.setState({ selectedOption });
console.log(`Option selected:`, selectedOption);
};
constructor(props) {
super(props)
this.state = {
selectedOption: null,
image: "",
slides: []
//(<img src = "https://i.postimg.cc/wjfpKgHg/Apple-1.png" className="fruit-images" alt="Pomegranate fruit" />)
}
this.onPick = this.onPick.bind(this);
}
componentDidMount = () => {
fetch('http://localhost/fruit_api/carousel_api.php')
.then((response) => response.json())
.then(data => {
console.log(data)
let slides = data.map((pic) => {
return(
<div key = {pic.img}>
<img src = {pic.img} />
</div>
)
})
this.setState({
slides: slides
})
console.log("state", this.state.slides);
})
.then(function(response){
console.log(response)
})
.catch(function(response){
console.error(response)
});
}
onPick = (image) => {
this.setState({image})
}
render() {
return (
<div className="caurosel-div">
<Carousel
arrows
slidesPerScroll={1}
slidesPerPage={3}
value={this.state.value}
slides = {this.state.slides}
onChange={this.onchange}
>
</Carousel>
<Dots value={this.state.value} onChange={this.onchange} number={this.state.slides.length} />
</div>
);
}
}
carousel.php
<?php
include_once('database/include.php');
$conn = mysqli_connect("localhost", "root", "", "farmer_portal");
if(!$conn)
die("Error connecting to DB". mysqli_connect_error());
if($_SERVER['REQUEST_METHOD'] == "GET")
{
$sel = "SELECT link from images";
$res = mysqli_query($conn, $sel);
if(mysqli_num_rows($res) > 0)
{
$users = array();
while($rows = mysqli_fetch_assoc($res))
{
$details=array(
"img" => $rows['link']
);
array_push($users, $details);
}
sendResponse(200,$users,'Images');
}
else
{
sendResponse(404,[],'Images not available');
}
}
?>
개발자 도구 이미지
문제가 데이터 할당에 있다고 생각했습니다. data
개체는 전체 응답 본문의 구성 및 때문에지도 기능이 작동하지 수. 이것을 작성하는 더 좋은 방법은
fetch('http://localhost/fruit_api/carousel_api.php')
.then((response) => response.json())
.then(data => {
const { data } = data // getting data attribute out of the response object
let slides = data.map((pic, k) => {
return(
<div key = {k}>
<img src = {pic.img} />
</div>
)
})
this.setState({
slides //this is interpreted as {slides: slides}
})
}).catch(function(response){
console.error(response)
});
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다