0

I have a list of images, I want to get the name of the image that is in the zero position of the matrix.

enter image description here

As you can see in the image, the name is inside File. But when I try items[0].file or items[0]['file'] the error TypeError returns: Cannot read property 'file' of undefined

My code:

export function Multiple() {

    const handleChange = files => {
      files.map(file => {
        setItems(item => [
          ...item,
          {
            file,
            onRemove: handleOnRemove,
            onClick: handleOnClick
          }
        ]);
      });
    };
  
    const handleOnRemove = file => {
      setItems(item => [...item.filter(item => item.file.name !== file.name)]);
    };
  
    const handleOnClick = file => {
      getBase64(file)
      setNameImage(file.name)
      setIsOpen(true)
    };
  
    const [items, setItems] = React.useState([]);
    const [isOpen, setIsOpen] = React.useState(false);
    const [nameImage, setNameImage] = React.useState('');
    const [codeImage, setCodeImage] = React.useState('');
  
    function getBase64(file) {
      var reader = new FileReader();
      reader.readAsDataURL(file);
      reader.onload = function () {
        setCodeImage(reader.result)
      };
      reader.onerror = function (error) {
        console.log('Error: ', error);
      };
    }
  
    React.useEffect(() => {
      return console.log(items[0]['file'])
    }, [items]);
  
  
    return (
      <>
        <File
          name="image"
          label="Upload de imagens"
          onChange={handleChange}
          required={true}
          multiple={true}
        >
          {items.map((item, index) => (
            <File.Item {...item} key={index} />
          ))}
        </File>
  
        <Modal
          title={nameImage}
          onClose={() => setIsOpen(false)}
          show={isOpen}
        >
          <img src={codeImage} alt='Imagem' className='image' />
        </Modal>
      </>
    );
  }
2
  • 2
    Can we have your component code ? Commented Jul 28, 2020 at 20:07
  • I put the code in the body of the question Commented Jul 28, 2020 at 20:09

1 Answer 1

2

When you first initialize items, it is an empty array so trying to log items[0].file will give error. Add undefined check. Also do not return it from useEffect. Return in useEffect is used for cleanup logic.

React.useEffect(() => {
 if(items[0] {
  console.log(items[0]['file'])
 }

}, [items]);
Sign up to request clarification or add additional context in comments.

4 Comments

Just curious, how can you make this dynamic without the use of [0] ?
What do you mean by dynamic?
suppose if op wanted to use index opposed to using [0] hardcoded? how would you go about finding the index key
This check was added to avoid the initial empty state. If you want to check for any random index you can store the index in the state and check for items[state.myDynamicIndex]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.