React native 如何在不渲染视图的情况下向react hook添加多个值
我在做一个项目 我想在useState钩子中添加多个图像,而无需重新设置钩子的状态 例如:我想使用相机拍摄一张图像,并用文件路径更新挂钩(目前还可以),然后我想通过相机拍摄另一张图像,并用以前的值更新react挂钩(需要以前的文件路径和新的文件路径)。 问题是,当我尝试捕获新图像并更新useState时,useState钩子会重新渲染视图,它会删除以前的文件路径并存储新的文件路径 我正遭受这个问题的困扰,请帮我找到解决办法。提前谢谢React native 如何在不渲染视图的情况下向react hook添加多个值,react-native,react-hooks,React Native,React Hooks,我在做一个项目 我想在useState钩子中添加多个图像,而无需重新设置钩子的状态 例如:我想使用相机拍摄一张图像,并用文件路径更新挂钩(目前还可以),然后我想通过相机拍摄另一张图像,并用以前的值更新react挂钩(需要以前的文件路径和新的文件路径)。 问题是,当我尝试捕获新图像并更新useState时,useState钩子会重新渲染视图,它会删除以前的文件路径并存储新的文件路径 我正遭受这个问题的困扰,请帮我找到解决办法。提前谢谢 const tempArray = []; cons
const tempArray = [];
const [selectedFiles, setSelectedFiles] = useState([]);
// capturing the image --------------
const chooseFile = () => {
const options = {
title: 'Select an option',
storageOptions: {
skipBackup: true,
path: 'images',
},
};
ImagePicker.showImagePicker(options, (response) => {
// console.log('Response = ', response);
if (response.didCancel) {
console.log('User cancelled image picker');
} else if (response.error) {
onsole.log('ImagePicker Error: ', response.error);
} else {
// let source = response;
// You can also display the image using data:
let source = {
uri: 'data:image/jpeg;base64,' + response.data
};
createImageArray(source);
}
});
};
const createImageArray = (imageData: string) => {
// adding file path to temp array -> to get the selected images
tempArray.push(imageData);
setImagesToHooks();
console.log("tempArray.length : " + tempArray.length);
}
const setImagesToHooks = () => {
const imagesArray = Array.from(tempArray).map((result) => result);
console.log('images : ' + JSON.stringify(imagesArray));
// adding mapped array to state hook
setSelectedFiles(imagesArray);
}
我找不到您使用的图像选择器。但我确实理解了问题所在,即问题在于更新阵列 问题是您正在使用第二个变量来存储以前的文件路径,而您可以使用状态本身来访问以前的文件路径 Es6引入了新的概念。 您可以通过本教程了解更多有关此概念的信息 代码参考:
const App = () => {
const [selectedFiles, setSelectedFiles] = useState([]);
const chooseFile = () => {
const options = {
title: 'Select an option',
storageOptions: {
skipBackup: true,
path: 'images',
},
};
ImagePicker.showImagePicker(options, (response) => {
if (response.didCancel) {
console.log('User cancelled image picker');
} else if (response.error) {
console.log('ImagePicker Error: ', response.error);
} else {
let source = {
uri: 'data:image/jpeg;base64,' + response.data,
};
setImagesToHooks(source);
}
});
};
const setImagesToHooks = (newImage) => {
// This will update the array. Refer the blog link for more information.
let imagesArray = [...selectedFiles, newImage];
setSelectedFiles(imagesArray);
};
// Return your code below
return <React.Fragment/>
};
const-App=()=>{
const[selectedFiles,setSelectedFiles]=useState([]);
const chooseFile=()=>{
常量选项={
标题:“选择一个选项”,
存储选项:{
skipBackup:没错,
路径:'图像',
},
};
ImagePicker.showImagePicker(选项,(响应)=>{
if(response.didconcel){
log('User cancelled image picker');
}else if(response.error){
log('ImagePicker错误:',response.Error);
}否则{
让源={
uri:'data:image/jpeg;base64'+response.data,
};
setImagesToHooks(源);
}
});
};
const setImagesToHooks=(newImage)=>{
//这将更新阵列。有关详细信息,请参阅博客链接。
让imagesArray=[…selectedFiles,newImage];
设置所选文件(imagesArray);
};
//在下面返回您的代码
返回
};
因为新的更新依赖于以前的状态,所以即使在重新渲染之后,状态仍会保持,而以前的路径也会保持不变
注意:我无法运行代码,因为没有提到image picker的包
如果需要,可以使用下面提到的选择器进行react native
更新你的代码,pls@CườngMạ恩古伊ễn、 更新。谢谢它在我的代码中帮助了我…@akshay。