未能将状态传递给其他路由器使用useNavigate。

huangapple go评论125阅读模式
英文:

Failed to pass state to other router with useNavigate

问题

你好,我有一个电子商务的React应用程序,想要在用户完成付款后,从路由“/cart”将状态数据传递到“/success”,为此我在我的购物车路由中使用了useNavigate,在成功路由中使用了useLocation来获取状态,但是在console.log之后,状态为null。我不知道如何解决这个问题,我需要帮助。以下是控制台日志:

哈希:""
键: "7e7cgplo"
路径名: "/success"
搜索:""
状态:null

我应该能在状态中看到我的Stripe付款数据(API正常运行)。

Cart.js

const Cart = () => {

    const cart = useSelector(state=>state.cart);
    const [ stripeToken, setStripeToken ] = useState(null);
    const navigate = useNavigate();
    
    const onToken = (token) => {
        setStripeToken(token);
    }

    console.log(stripeToken)

    useEffect(() => {
        const makeRequest = async () => {
            try{
                const response = await userMethod.post("/test", {
                    tokenId: stripeToken.id,
                    amount: 500,
                })
                navigate("/success", { data: response.data });
            } catch (err) {
            } 
        }
        stripeToken &&  makeRequest();
    }, [stripeToken, cart.total, navigate])

    // 其他代码...

Success.js

import React from 'react'
import { useLocation } from 'react-router-dom'

const Success = () => {
  
  const location = useLocation();

  console.log(location)

  return (
    <div>成功!!</div>
  )
}

export default Success
英文:

Hello i have a ecommerce react application and want to pass my state data from router "/cart" to "/success" after user finish the payment, to do that i use useNavigate in my cart route and in the success route i use useLocation to get the state, but after console.log the state is null. I have no clue to solve this problem, i need help Thanks.

This is the console log :

hash: &quot;&quot;
key: &quot;7e7cgplo&quot;
pathname: &quot;/success&quot;
search: &quot;&quot;
state: null

I should be able to see my stipe payment data in the state(the api is working fine)

Cart.js

const Cart = () =&gt; {

    const cart = useSelector(state=&gt;state.cart);
    const [ stripeToken, setStripeToken ] = useState(null);
    const navigate = useNavigate();
    
    const onToken = (token) =&gt; {
        setStripeToken(token);
    }

    console.log(stripeToken)

    useEffect(() =&gt; {
        const makeRequest = async () =&gt; {
            try{
                const response = await userMethod.post(&quot;/test&quot;, {
                    tokenId: stripeToken.id,
                    amount: 500,
                })
                navigate(&quot;/success&quot;, { data: response.data });
            } catch (err) {
            } 
        }
        stripeToken &amp;&amp;  makeRequest();
    }, [stripeToken, cart.total, navigate])



  return (
    &lt;Container&gt;
        &lt;Navbar /&gt;
        &lt;Wrapper&gt;
            &lt;Title&gt;Your Bag&lt;/Title&gt;
            &lt;Top&gt;
                &lt;TopButton&gt;Continue Shopping&lt;/TopButton&gt;
                &lt;TopTexts&gt;
                    &lt;TopText&gt;Shooping Bag({cart.quantity})&lt;/TopText&gt;
                    &lt;TopText&gt;Your Wishlist&lt;/TopText&gt;
                &lt;/TopTexts&gt;
                &lt;TopButton type=&quot;filled&quot;&gt;Checkout Now&lt;/TopButton&gt;
            &lt;/Top&gt;
            &lt;Bottom&gt;
                &lt;Info&gt;
                {cart.products.map((product) =&gt; (
                    &lt;Product&gt;
                        &lt;ProductDetail&gt;
                            &lt;Image src={product.img} alt=&quot;google&quot;&gt;&lt;/Image&gt;
                            &lt;Details&gt;
                                &lt;ProductName&gt;&lt;b&gt;Product:&lt;/b&gt; {product.title}&lt;/ProductName&gt;
                                &lt;ProductId&gt;&lt;b&gt;ID:&lt;/b&gt; {product._id}&lt;/ProductId&gt;
                                {/* &lt;ProductColor color=&quot;black&quot; /&gt;
                                &lt;ProductSize&gt;&lt;b&gt;Size:&lt;/b&gt; XL&lt;/ProductSize&gt; */}
                            &lt;/Details&gt;
                        &lt;/ProductDetail&gt;
                        &lt;PriceDetail&gt;
                            &lt;ProductAmountContainer&gt;
                                &lt;AiOutlineMinus /&gt;
                                &lt;ProductAmount&gt;{product.quantity}&lt;/ProductAmount&gt;
                                &lt;GrAdd /&gt;
                            &lt;/ProductAmountContainer&gt;
                            &lt;ProductPrice&gt;$ {product.price * product.quantity}&lt;/ProductPrice&gt;
                        &lt;/PriceDetail&gt;
                    &lt;/Product&gt;
                        ))}
                    &lt;Hr&gt;&lt;/Hr&gt;
                &lt;/Info&gt;
                &lt;Summary&gt;
                    &lt;SummaryTitle&gt;Order Summary&lt;/SummaryTitle&gt;
                        &lt;SummaryItem&gt;
                            &lt;SummaryItemText&gt;Subtotal&lt;/SummaryItemText&gt;
                            &lt;SummaryItemPrice&gt;$ {cart.total}&lt;/SummaryItemPrice&gt;
                        &lt;/SummaryItem&gt;
                        &lt;SummaryItem&gt;
                            &lt;SummaryItemText&gt;Estimated Shipping&lt;/SummaryItemText&gt;
                            &lt;SummaryItemPrice&gt;$ 5&lt;/SummaryItemPrice&gt;
                        &lt;/SummaryItem&gt;
                        &lt;SummaryItem&gt;
                            &lt;SummaryItemText&gt;Shipping Discount&lt;/SummaryItemText&gt;
                            &lt;SummaryItemPrice&gt;$ -6&lt;/SummaryItemPrice&gt;
                        &lt;/SummaryItem&gt;
                        &lt;SummaryItem&gt;
                            &lt;SummaryItemText type=&quot;total&quot;&gt;Total&lt;/SummaryItemText&gt;
                            &lt;SummaryItemPrice&gt;$ {cart.total}&lt;/SummaryItemPrice&gt;
                        &lt;/SummaryItem&gt;
                        &lt;StripeCheckout 
                            name=&quot;Kimia shop&quot; 
                            image=&quot;https://d3o2e4jr3mxnm3.cloudfront.net/Mens-Jake-Guitar-Vintage-Crusher-Tee_68382_1_lg.png&quot;
                            billingAddress
                            shippingAddress
                            description={`your total is $ ${cart.total}`}
                            amount={cart.total * 100}
                            token={onToken}
                            stripeKey={KEY}
                            &gt;
                            &lt;Button&gt;Checkout Now&lt;/Button&gt;
                        &lt;/StripeCheckout&gt;
                &lt;/Summary&gt;
            &lt;/Bottom&gt;
        &lt;/Wrapper&gt;
        &lt;Footer /&gt;
    &lt;/Container&gt;
  )
}

export default Cart

Success.js

import React from &#39;react&#39;
import { useLocation } from &#39;react-router-dom&#39;

const Succes = () =&gt; {
  
  const location = useLocation();

  console.log(location)

  return (
    &lt;div&gt;Succes!!&lt;/div&gt;
  )
}

export default Succes

答案1

得分: 1

在你的示例中问题在于你将数据传递为 data,而应该是 state

navigate('/success', { state: response.data })
英文:

In your example the issue is that you are passing the data as data, it should be state instead:

navigate(&#39;/success&#39;, { state: response.data })

huangapple
  • 本文由 发表于 2023年4月17日 14:39:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76032316.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定