Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Return to parent component if error received from post request

I have a parent component that makes a call to an external service onClick.

When the onClick happens, it sets the showPlaceholder component to true, which shows a placeholder whilst the external request is happening. If the request is successful the user is directed to their dashboard. If the request is unsuccessful, I want the placeholder component to not show and the parent component to render the error message.

Everything is working as expected but I can’t remove the placeholder when an error is raised. Any advice?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

    const ParentComponent = () => {

    const [idNumber, setidNumber] = useState("");
    const [showPlaceholder, setShowPlaceholder] = useState(false);

    const OnClickHandler = () => {
        setShowPlaceholder(true);
        we post the data here and start the request
        
    };

    return (
        <>
            {showPlaceholder ? <showPlaceholder error={error} /> :
                <>
                    <input
                        value={idNumber}
                        onChange={idNumber => setidNumber((idNumber))} />
                    <button
                        onClick={OnClickHandler}
                    >
                        Continue
                    </button>

                    {error && <p>errorMessage</p>}
                </>
            }
        </>
    );
};

export default ParentComponent;

Child Component

const Placeholder = (error) => {
    if (error === true) {
        cancelOnClickHandler();
    }

    const onClickHandler = () => {
        STOP SHOWING THE PLACEHOLDER
        
    };

    return (
        <div >
            <div>
                Loader...
            </div>
            <Link
                onClick={onClickHandler}
            >
                cancel
            </Link>
        </div>
    );
};

export default Placeholder;

>Solution :

There are a couple of mistakes in your code:

  1. All react components’ names should we written using PascalCase:
{showPlaceholder ? <ShowPlaceholder error={error} /> // <ShowPlaceholder /> instead of <showPlaceholder />
  1. Below you pass error variable as a prop to ShowPlaceholder, but no such variable exists in your code
{showPlaceholder ? <ShowPlaceholder error={error} /> // no error varaible exists in your scope
  1. You are not destructuring your prop inside of ShowPlaceholder component
// either            v here v
const Placeholder = ({ error }) => {
    if (error === true) {
        ... your code
// or
const Placeholder = (props) => {
    // v here v
    if (props.error === true) {
        ... your code

To remove your ShowPlaceholder when error is raised, you need to set your showPlaceholder variable to false using setShowPlaceholder when your external service returns error response, but you didn’t supply code for it so I can’t help you.
But if you want to close ShowPlaceholder with your button, do this:

// in Parent component, add this prop:
<ShowPlaceholder error={error} hidePlaceholder={() => setShowPlaceholder(false)} />

const Placeholder = (props) => {
    // ... code ...

    const onClickHandler = () => {
        props.hidePlaceholder()
    };

    // ... code ...
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading