您好,我新来的反应,我正在制作一个模因发生器。我试图将其从类转换为反应挂钩,不确定为什么它不起作用。到目前为止,这就是我所拥有的,当我尝试生成新图像时出现错误。
import React, { useEffect, useState } from "react"
function MemeGenerator() {
const [newText, setNewText] = useState({
topText: "",
lastName: ""
})
const [randomImg, setrandomImg] = useState("http://i.imgflip.com/1bij.jpg")
const [allMemeImgs, setallMemeImages] = useState([])
useEffect(()=> {
fetch("https://api.imgflip.com/get_memes")
.then(response => response.json())
.then(response => {
const {memes} = response.data
setallMemeImages({allMemeImgs: memes})
})
},[])
function handleChange(event) {
const {name, value} = event.target
setNewText({
...newText,
[name]: value
})
}
// error is on this function I believe
function handleSubmit(event) {
event.preventDefault()
const randNum = Math.floor(Math.random() * allMemeImgs.length)
alert(randNum)
const randMemeImg = allMemeImgs[randNum].url
setrandomImg(randMemeImg)
}
return (
<div>
<form className="meme-form" onSubmit={handleSubmit}>
<input
type="text"
name="topText"
placeholder="Top Text"
value={newText.topText}
onChange={handleChange}
/>
<input
type="text"
name="bottomText"
placeholder="Bottom Text"
value={newText.bottomText}
onChange={handleChange}
/>
<button>Gen</button>
</form>
<div className="meme">
<img src={randomImg} alt="" />
<h2 className="top">{newText.topText}</h2>
<h2 className="bottom">{newText.bottomText}</h2>
</div>
</div>
)
}
我以前工作的是:
import React, {Component} from "react"
class MemeGenerator extends Component {
constructor() {
super()
this.state = {
topText: "",
bottomText: "",
randomImg: "http://i.imgflip.com/1bij.jpg",
allMemeImgs: []
}
this.handleChange = this.handleChange.bind(this)
this.handleSubmit = this.handleSubmit.bind(this)
}
componentDidMount() {
fetch("https://api.imgflip.com/get_memes")
.then(response => response.json())
.then(response => {
const {memes} = response.data
this.setState({ allMemeImgs: memes })
})
}
handleChange(event) {
const {name, value} = event.target
this.setState({ [name]: value })
}
handleSubmit(event) {
event.preventDefault()
const randNum = Math.floor(Math.random() * this.state.allMemeImgs.length)
const randMemeImg = this.state.allMemeImgs[randNum].url
this.setState({ randomImg: randMemeImg })
}
render() {
return (
<div>
<form className="meme-form" onSubmit={this.handleSubmit}>
<input
type="text"
name="topText"
placeholder="Top Text"
value={this.state.topText}
onChange={this.handleChange}
/>
<input
type="text"
name="bottomText"
placeholder="Bottom Text"
value={this.state.bottomText}
onChange={this.handleChange}
/>
<button>Gen</button>
</form>
<div className="meme">
<img src={this.state.randomImg} alt="" />
<h2 className="top">{this.state.topText}</h2>
<h2 className="bottom">{this.state.bottomText}</h2>
</div>
</div>
)
}
错误提示: TypeError:未定义不是对象(正在评估“ allMemeImgs [randNum] .url”)
答案 0 :(得分:0)
您希望将数组初始化为 allMemeImgs 状态
const [allMemeImgs, setallMemeImages] = useState([])
但已将其配置为具有此特定 useEffect 挂钩
的对象useEffect(()=> {
fetch("https://api.imgflip.com/get_memes")
.then(response => response.json())
.then(response => {
const {memes} = response.data
setallMemeImages({allMemeImgs: memes}) // culprit here
})
},[])
在线提问:
setallMemeImages({allMemeImgs: memes})
建议将此行更改为:
setallMemeImages([...memes]);
答案 1 :(得分:0)
const [allMemeImgs, setallMemeImages] = useState([]);
allMemeImgs
是处于本地状态的数组。但是在setallMemeImages({allMemeImgs: memes})
,您将其设置为对象。
将其更改为setallMemeImages((prev) => [...prev, memes])
答案 2 :(得分:0)
注意您也可以使用setallMemeImages([...memes]);
,但是如果遇到类型错误,请使用setallMemeImages(memes);
,如果遇到randMemeImg
的类型从不出错,请使用:
const randMemeImg = allMemeImgs[randNum];
setrandomImg(Object(randMemeImg).url);
function MemeGenerator() {
const [newText, setNewText] = React.useState({
topText: "",
bottomText: ""
})
const [randomImg, setrandomImg] = React.useState("http://i.imgflip.com/1bij.jpg")
const [allMemeImgs, setallMemeImages] = React.useState([])
React.useEffect(()=> {
fetch("https://api.imgflip.com/get_memes")
.then(response => response.json())
.then(response => {
const {memes} = response.data
setallMemeImages([...memes]);
})
},[])
function handleChange(event) {
const {name, value} = event.target
setNewText({
...newText,
[name]: value
})
}
function handleSubmit(event) {
event.preventDefault()
const randNum = Math.floor(Math.random() * allMemeImgs.length)
alert(randNum)
const randMemeImg = allMemeImgs[randNum].url
setrandomImg(randMemeImg)
}
return (
<div>
<form className="meme-form" onSubmit={handleSubmit}>
<input
type="text"
name="topText"
placeholder="Top Text"
value={newText.topText}
onChange={handleChange}
/>
<input
type="text"
name="bottomText"
placeholder="Bottom Text"
value={newText.bottomText}
onChange={handleChange}
/>
<button>Gen</button>
</form>
<div className="meme">
<img src={randomImg} alt="" />
<h2 className="top">{newText.topText}</h2>
<h2 className="bottom">{newText.bottomText}</h2>
</div>
</div>
)
}
ReactDOM.render(<MemeGenerator />, document.querySelector('#app'));
<script src="https://unpkg.com/react@16.7.0-alpha.0/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@16.7.0-alpha.0/umd/react-dom.development.js"></script>
<div id="app"></div>