[英]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.
在 Payment 按钮中,我调用 axios 来更新状态,然后我需要父级显示这个新的更新状态,所以我尝试将“false”状态设置为 true,我希望这会触发重新渲染。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.