0

I would like when I open a modal only launch a service if the modal is open, and re-use the service data for another injection in another component.

Here is the component :

import React from 'react';
import { Modal } from 'react-bootstrap';
import FormResolveDispute from '../forms/form-resolve-dispute';
import { useGetAllMovementsByTiersQuery } from '../../services/EventApi';

const ModalResolveEvent = (props: any) => {
const {show, onClose, dispute} = props;
if (show) {
    const {mouvement} = useGetAllMovementsByTiersQuery(
        {
            qp: new URLSearchParams({
                eventId: dispute.id,
            }).toString(),
        },
        {
            selectFromResult: ({data}) => ({
                movement: data?.items || [],
            }),
        }
    );
    return data;
}
return (
    <>
        <Modal show={show} onHide={onClose} centered={true} scrollable={true}>
            <Modal.Header closeButton>
                <Modal.Title className="text-title">
                    Test
                </Modal.Title>
            </Modal.Header>
            <Modal.Body>
                <FormResolveDispute
                    mvts={mouvement}
                    submitCallback={onClose}
                />
            </Modal.Body>
        </Modal>
    </>
   );
 };

 export default ModalResolveEvent;

here I check if show is true to make the call

 if (show) {
   const {movement} = useGetAllMovementsByTiersQuery(
    {
        qp: new URLSearchParams({
            eventId: dispute.id,
        }).toString(),
    },
    {
        selectFromResult: ({data}) => ({
            movement: data?.items || [],
        }),
    }
);
return movement;

}

and I would like to inject the movement into the FormResolveDispute component.

   <FormResolveDispute mvts={movement} submitCallback={onClose} />

But I have the following error message

TS2304: Cannot find name 'movement'.

I would like to retrieve the movement generated in the conditonnal to send it back to the FormResolveEvent

Could you tell me where the problem is or if there is another way ?

2
  • TS2304: Cannot find name 'movement'. It's because movement is inside the scope you must aware. In my opinion you can save the movement to state. Commented Oct 8, 2021 at 23:49
  • @edhi.uchiha Thank you for your answer, but how to implement this change ? Commented Oct 11, 2021 at 8:45

1 Answer 1

1

Please try this.

// create initial state
const [movement, setMovement] = useState([]);

if (show) {
const { movement } = useGetAllMovementsByTiersQuery(
    {
        qp: new URLSearchParams({
            eventId: dispute.id,
        }).toString(),
    },
    {
        selectFromResult: ({data}) => ({
            movement: data?.items || [],
        }),
    }
   );

// set state
setMovement(movement);
}

So in this component, movement props is get from state

<FormResolveDispute
     mvts={movement}
     submitCallback={onClose}
     />
Sign up to request clarification or add additional context in comments.

4 Comments

thank you for the answer but the modification gives me an error message: React Hook "useGetAllMovementsByTiersQuery" cannot be called inside a callback. React Hooks must be called in a React function component or a custom React Hook function. I think a react-hook cannot be used in conditional
Are you use class or functional component ?
functionnal component
hm but the problem not because hook used in conditional

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.