React state updating but not rendering on child component

Tayler Hughes

I know the state is updating because 1. the 'Loading...' is going away, I can console log this.state.images to see the array. However when the state updates and the loading goes the searchbar shows up but the Card's within CardList do not.

They do show up when I search for a correct string, but not before.

If I pass this.state.images to CardList they show up perfectly. However when I move to the filteredImages they only show up when filtered.

Any ideas? Thanks in advance.

class App extends Component {

  constructor() {
    super();
    this.state = {
      images:[],
      searchfield: ''
    }
  }

  getLabels = (image) => {
    const AuthKey = key.key;
    const res = axios.post(`https://vision.googleapis.com/v1/images:annotate?key=${AuthKey}`, {
      requests: [
        {
          image:{
            source:{
              imageUri: `http://storage.googleapis.com/${image}`
            }
          },
          features:[
            {
              type:"LABEL_DETECTION",
              maxResults:10
            }
          ]
        }
      ]
    });

    res.then(function (response) {
      const results = response.data.responses[0].labelAnnotations;
      const ex = results.map(result => {
        return result.description;
      }); 
      return ex;
    });

    return res;

  };

  componentDidMount() {
      imageFiles.imageFiles.forEach(img => {
        this.getLabels(img).then(result => {
          const results = result.data.responses[0].labelAnnotations;
          const labels = results.map(result => {
          return result.description;
        });
        //Add new values to the state 
        this.setState({images:[...this.state.images, {img, labels}]});
      });
    })
  }

  onSearchChange = (event) => {
    this.setState({searchfield: event.target.value});
  }

  render() {
    const filteredImages = this.state.images.filter(image => {
      return image.labels.includes(this.state.searchfield.toLowerCase());
    });
    // Create an array of objects to store the image path and the labels detected from Google Vision
    if (this.state.images.length === 0) {
      return <h1>Loading...</h1>
    } else {
      return (
        <Grid className="App">
          <SearchBox searchChange={this.onSearchChange}/>
          <CardList images={filteredImages} />
        </Grid>
      )}
  }

}

export default App;
Manoj
 class App extends Component {

      constructor() {
        super();
        this.state = {
          images:[],
          searchfield: '',
          filteredImages:[]
        }
      }

      getLabels = (image) => {
        const AuthKey = key.key;
        const res = axios.post(`https://vision.googleapis.com/v1/images:annotate?key=${AuthKey}`, {
          requests: [
            {
              image:{
                source:{
                  imageUri: `http://storage.googleapis.com/${image}`
                }
              },
              features:[
                {
                  type:"LABEL_DETECTION",
                  maxResults:10
                }
              ]
            }
          ]
        });

        res.then(function (response) {
          const results = response.data.responses[0].labelAnnotations;
          const ex = results.map(result => {
            return result.description;
          }); 
          return ex;
        });

        return res;

      };

      componentDidMount() {
          imageFiles.imageFiles.forEach(img => {
            this.getLabels(img).then(result => {
              const results = result.data.responses[0].labelAnnotations;
              const labels = results.map(result => {
              return result.description;
            });
            //Add new values to the state 
            this.setState({images:[...this.state.images, {img, labels}]});
this.setState({filteredImages:[...this.state.images, {img, labels}]});
          });
        })
      }

      onSearchChange = (event) => {
        this.setState({searchfield: event.target.value});
        let filteredImages = this.state.images.filter(image => {
            return image.labels.includes(this.state.searchfield.toLowerCase());
          });
        this.setState({filteredImages});
      }

      render() {

        // Create an array of objects to store the image path and the labels detected from Google Vision
        if (this.state.images.length === 0) {
          return <h1>Loading...</h1>
        } else {
          return (
            <Grid className="App">
              <SearchBox searchChange={this.onSearchChange}/>
              <CardList images={this.state.filteredImages} />
            </Grid>
          )}
      }

    }

    export default App;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

React component is rendering but not updating when state is updating

React not re-rendering child component after updating state var passed as prop

React: Child component not rendering on parents state change

Updating React child component after state change

Updating state with props on React child component

React not updating state on Child Component load

Updating Parent Component State from Child Component with UseState React Hook

React JS: State Changes on Child component updating the Parent Component

Updating state without rendering the whole React component (useState)

React Component does not re rendering when updating an object in state hooks

React Child Component not re-rendering mapped state

React child component not re-rendering on state change

React child component not re-rendering on state update

Child component not updating in React

React state is updating but the component is not

Problem updating parent state from child component in React

React child component state not updating when new props are passed

React Child Component Not Updating After Parent State Change

react child component not updating after parent state changed

Child component not updating on state change

Parent state not updating child component

Updating state before rendering (React)

React State not updating before the rendering

Updating state from child component to parent component

updating react component state using this.setState isn't re-rendering the component

Rendering a dynamic child component in React

React component not rendering on state update

React-Redux : Child component not re-rendering on parent state change when using connect() in child

why child components div in react is not re rendering even though child component re renders at state change