[英]React TypeScript button with loading spinner
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. 我在react组件内部有一个按钮,单击按钮时的测试显示Join Now
,单击按钮后,我调用函数submitForm
,然后将状态设置为true。 I want the loading svg image to be displayed but instead the path to the image is shown on the button. 我希望显示正在加载的svg图像,但是该图像的路径显示在按钮上。
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};
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. 尝试以这种方式显示图像-您需要一个img标记才能显示图像内容,React不会开箱即用。
return (
<button className="btn-join" onClick={submitForm}>
{!state.loading && 'Join Now!'}
{state.loading && <img src={loading} /> }
</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!' : <img src={loading} alt="loading" />}
</button>
);
}
export {ButtonSpinner};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.