如何添加react(JS)中的地图返回的图像
我正在构建一个聊天框,在卡上,我有一个我用messagelist
用usestate
:
但是它没有显示图像(我正在更改“ Messagelist” func):
函数convboard({name}){
const [initMessageList, setMessageList] = useState([])
const messageList = initMessageList.map((now, key) => {
if (now.Image) {
return <img src={preview} />
}
return <Message text={now.text} key={key} />
});
let newText = useRef(null);
const addMessage = () => {
if (newText.current.value != "") {
setMessageList([...initMessageList, {
text: newText.current.value,
key: initMessageList.length
}])
newText.current.value = ""
}
}
const onKeyFunc = function onKeyEnter(e) {
if (e.key === "Enter" && newText.current.value != "") {
{ addMessage() };
}
}
const [image, setImage] = useState();
const [preview, setPreview] = useState();
const pressRef = useRef();
useEffect(() => {
if (image) {
const reader = new FileReader()
reader.onloadend = () => {
setPreview(reader.result)
};
reader.readAsDataURL(image)
setMessageList([...initMessageList, {
text: preview,
key: initMessageList.length
}])
} else {
setPreview(null)
}
}, [image]);
return (
<Tab.Pane eventKey={name}>
<Card className='card'>
<extraWarper className="extra">
{messageList}
</extraWarper>
</Card>
<InputGroup>
<FormControl className='inputLine' ref={newText}
placeholder="your text" onKeyPress={onKeyFunc}
/>
<Button variant="outline-secondary">record</Button>
<DropdownButton title="upload" variant="outline-secondary">
<button onClick={(event) =>
pressRef.current.click()
}>
Send image
</button>
<input ref={pressRef} id="filePicker" style={{ display: "none" }} type={"file"}
onChange={(event) => {
const file = event.target.files[0]
if (file) {
setImage(file)
} else {
setImage(null)
}
}
} />
</DropdownButton>
<Button variant="outline-secondary" onClick={addMessage}>send</Button>
</InputGroup>
</Tab.Pane>
);
导出默认的探报板 }
但是,当我尝试添加图像空白时。 有什么想法吗? (对不起,这一团糟,谢谢!)
I'm building a chatbox, on a card I have a messageList
that I made with useState
:
I'm trying to add images to the initMessageList to be displayed on the card with preview
using useRef
but it does not show the image (I'm changing the 'messageList' func):
function ConvBoard({ name }) {
const [initMessageList, setMessageList] = useState([])
const messageList = initMessageList.map((now, key) => {
if (now.Image) {
return <img src={preview} />
}
return <Message text={now.text} key={key} />
});
let newText = useRef(null);
const addMessage = () => {
if (newText.current.value != "") {
setMessageList([...initMessageList, {
text: newText.current.value,
key: initMessageList.length
}])
newText.current.value = ""
}
}
const onKeyFunc = function onKeyEnter(e) {
if (e.key === "Enter" && newText.current.value != "") {
{ addMessage() };
}
}
const [image, setImage] = useState();
const [preview, setPreview] = useState();
const pressRef = useRef();
useEffect(() => {
if (image) {
const reader = new FileReader()
reader.onloadend = () => {
setPreview(reader.result)
};
reader.readAsDataURL(image)
setMessageList([...initMessageList, {
text: preview,
key: initMessageList.length
}])
} else {
setPreview(null)
}
}, [image]);
return (
<Tab.Pane eventKey={name}>
<Card className='card'>
<extraWarper className="extra">
{messageList}
</extraWarper>
</Card>
<InputGroup>
<FormControl className='inputLine' ref={newText}
placeholder="your text" onKeyPress={onKeyFunc}
/>
<Button variant="outline-secondary">record</Button>
<DropdownButton title="upload" variant="outline-secondary">
<button onClick={(event) =>
pressRef.current.click()
}>
Send image
</button>
<input ref={pressRef} id="filePicker" style={{ display: "none" }} type={"file"}
onChange={(event) => {
const file = event.target.files[0]
if (file) {
setImage(file)
} else {
setImage(null)
}
}
} />
</DropdownButton>
<Button variant="outline-secondary" onClick={addMessage}>send</Button>
</InputGroup>
</Tab.Pane>
);
export default ConvBoard
}
but when i try to add an Image its blank.
Any ideas?
(Sorry for the mess, and thank you!)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
此代码中没有
now.image
。可能有
now.text
,代表now.image
andpreview
在代码中,因为您将其设置在代码的这一部分中
There is no
now.Image
in this code.there may be
now.Text
which representsnow.Image
andpreview
in your codebecause you set it in this part of code