1

I have a button inside a react component, the test on the button says Join Now when the button is clicked I call the function submitForm which then sets the state to true. I want the loading svg image to be displayed but instead the path to the image is shown on the button.

import React, { useState } from 'react';
import Context from './context';
import loading from '../images/loading.svg';

const ButtonSpinner: React.FC = () => {

  const [state, setState] = useState({loading: false});
  function submitForm() {
    setState({ ...state, loading: true})
  }

  return (
     <button className="btn-join" onClick={submitForm}>
       {!state.loading && 'Join Now!'} 
       {state.loading && loading}
     </button>       
  );
}

export {ButtonSpinner};
1
  • You need to use the img tag to show the image. Commented Sep 3, 2019 at 5:39

2 Answers 2

4

try displaying image like this instead- you need an img tag in order to display image content, React won't do that out of the box.

return (
  <button className="btn-join" onClick={submitForm}>
    {!state.loading && 'Join Now!'} 
    {state.loading && <img src={loading} /> }
  </button>       
);
Sign up to request clarification or add additional context in comments.

Comments

0
import React, { useState } from 'react';
import Context from './context';
import loading from '../images/loading.svg';

const ButtonSpinner: React.FC = () => {

const [state, setState] = useState({loading: false});
function submitForm() {
    setState({ ...state, loading: true})
}

return (
 <button className="btn-join" onClick={submitForm}>
   {!state.loading ? 'Join Now!' : <img src={loading} alt="loading" />} 
 </button>       
);
}

export {ButtonSpinner};

1 Comment

needs the closing image tag />

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.