Using data retrieved by axios.get('external_url')

Logging response.data outputs the HTML I need to display my header however, I can’t find a way to access the response outside the get function. I’m pretty sure I need to find a way to add the data to the state but I can’t get it working.

Any help would be appreciated.

const Header = ({ state }) => {

  axios.get('external_url')
  .then(function (response) {
    console.log(response.data);
  })
  .catch(function (error) {
    console.log(error);
  });
  
  return (
    <>
           {/* Where i want to output response.data */}
    </>
  );
};

export default connect(Header);

Hi @leogardner12

Welcome to the Frontity community. If you’re getting the data you could add, for example, a headerContent property to your state and store it there so that it could be accessed from other components.

index.js:

state: {
    theme: {
      menu: [],
      headerContent: '',
    ...
}

header.js:

axios.get(‘external_url’)
.then(response => response.json())
.then(data =>  {
   state.theme.headerContent = data
})
1 Like