简体   繁体   中英

Issue trying to re-render react parent component from child

Trying to re-render parent component from child component callback . in the Payment button I call axios to update a state, then I need the parent to show this new updated state, so I try to set a "false" state to true, which I am hoping will trigger a re-render.

Error:

Line 28:4:  Expected an assignment or function call and instead saw an expression

from parent


    const [success, setSuccess] = useState(false);

<h4>
                    Purchase 5 more post's : <PaymentButton name="more post" price={5} description="create more post"
                        object={
                { email: user.email, max_posts: user.max_posts + 5 }
                        }
                        success ={()=>setSuccess(true)}

                    />

                </h4>

from child

const PaymentButton = ({ name, price, description,object }) => {

    const handleToken = async (token, addresses) => {
        const response = await axios.post('http://localhost:3000/api/payment/checkout', {
            token,
            product: { name, price, description }
        });

        const { status , resolved} = response.data;

        console.log('Response:', response);
        console.log('network_status :', resolved);
        console.log(JSON.stringify(object))


        if (status === 'succeeded' && resolved ==='approved_by_network') {
            toast('Success! Purchase Approved. ', { type: 'success' });
            return axios.put('http://localhost:3000/api/users/getuser', { ...object });
            object.success


        } else {
            toast('Something went wrong, contact site admin about this', { type: 'error' });
        }
    };


您可能需要通过编写来反转真实状态:

!success

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM