如何使组件从另一个组件引起组件

发布于 2025-01-17 11:33:33 字数 4629 浏览 0 评论 0原文

组件IM试图从:配置

    import React from 'react'
    import {useContext} from "react"
    import { UserNameContext } from '../App'
    import PersonIcon from '@mui/icons-material/Person';
    import { useNavigate } from 'react-router-dom';
    import "./Profile.css"
    import axios from 'axios';
    import { useState } from 'react'
    import { useReducer } from "react"
    const EditProfilePage = () => {
 
  const [rerender, setRerender] = useState(false);
   

  const Navigate = useNavigate()
  const [newusername,setnewusername] = useState("")
    const userName = useContext(UserNameContext)
   

  function updateUser(e){
    e.preventDefault()
    console.log(userName)
    console.log(newusername)
    axios.post("http://localhost:5000/profile/editProfile",({username:userName,nusername:newusername}),{headers:{
      "Content-Type" : "application/json"
    }}).then((res)=>{ localStorage.setItem("token",res.data.token) 
    console.log(res.data)
   console.log(localStorage.getItem("token"))
    Navigate(`/${newusername}`)
   
    
  
  } )
  }
function reRenderf(){
  setRerender(!rerender)
}
  return (

    <div className='Wrapper'>

    <img/>

    <PersonIcon/>
 
  
    
    <form onSubmit={updateUser}>
    <img  />
    <input placeholder="name" value={newusername} onChange={e => setnewusername(e.target.value)} / >

    <input placeholder='Bio' />
    <button onClick={reRenderf}  >Render</button>
    <button type='Submit' >Update</button>
    </form>

    </div>

  )
}

export default EditProfilePage

文件组件:

import axios from "axios"
import { useEffect } from "react"
import { useState } from "react"
import "./Profile.css"
import styled from "styled-components"
import { ProfilePicture } from '../App'
import React from 'react'
import {useContext} from "react"
import { UserNameContext } from '../App'
import PersonIcon from '@mui/icons-material/Person';
import { useNavigate } from 'react-router-dom';
import "./Profile.css"


import { useReducer } from "react"
import EditProfilePage from './EditProfilePage'
const Wrapper = styled.div`
display: flex;
background-color: black;
height: 100vh;

justify-content: center;

`
const Container = styled.div`
background-color: gray;
width: 50%;

`
const Status = styled.div`
display: flex;
font-size: 15px;
`

const H4 = styled.h4`
padding: 10px;

`
const EditProfile = styled.button`

`
const EditProfilePopup = styled.div`
background-color: rgba(0,0,0,0.7);
position: fixed;
height: 50%;


left:40%;
`

    export function Profile (){
      //mAKE SURE ITS NOT UNDEFINED
     const userdata = React.useContext(ProfilePicture)
        const pathname = window.location.pathname
        const[data,setData] = useState([])
        const [user,isUser] = useState(false)
        const[owner,isOwner] = useState(false)
        const[editProfile,seteditProfile] = useState(false)
    
      let username = pathname.split("/")[1]
    
      useEffect(()=>{
        axios.post('http://localhost:5000/profile/getProfile', {
          "username":username
        })
        .then((res)=> {
        
            setData(res.data[0])
            isUser(true)
         
           
        }).catch((err)=> {
           
            isUser(false)
        })
        
      
    
      }, []);
      function checkifOwner(){
        if (userdata.username == username){
         
          isOwner(true)
        }
        else{
       
        
        }
    
      }
      
      setTimeout(checkifOwner,10)
      function editheProfile(){
        seteditProfile(!editProfile)
      }
      return (
    <Wrapper>
    <Container>
    
    
        <div className='Profile'>
        {owner ? <EditProfile onClick={editheProfile} >Edit Profile</EditProfile>:console.log("not Owner") }
        <h2>{data.username}</h2>
        {editProfile ? <EditProfilePopup><EditProfilePage/></EditProfilePopup> : null}
    <img className='ProfileImg' src='https://cdn.pixabay.com/photo/2015/10/05/22/37/blank-profile-picture-973460_1280.png' />
    {user?null:<h2>No User Exists</h2>}
      
        <img  />
        <Status>
    <H4>Followers</H4>
    <H4>Following</H4>
    </Status>
    <H4>Posts</H4>
    
        </div>
       
      
        </Container>
        </Wrapper>
    
      )
    }

我试图从我的EditProfilePage组件中恢复配置文件组件 我该如何实现? 使所有一个组件都可以解决这个问题,只是想知道是否还有其他方法可以解决该问题?

The Component im trying to reRender The Profile Component From:

    import React from 'react'
    import {useContext} from "react"
    import { UserNameContext } from '../App'
    import PersonIcon from '@mui/icons-material/Person';
    import { useNavigate } from 'react-router-dom';
    import "./Profile.css"
    import axios from 'axios';
    import { useState } from 'react'
    import { useReducer } from "react"
    const EditProfilePage = () => {
 
  const [rerender, setRerender] = useState(false);
   

  const Navigate = useNavigate()
  const [newusername,setnewusername] = useState("")
    const userName = useContext(UserNameContext)
   

  function updateUser(e){
    e.preventDefault()
    console.log(userName)
    console.log(newusername)
    axios.post("http://localhost:5000/profile/editProfile",({username:userName,nusername:newusername}),{headers:{
      "Content-Type" : "application/json"
    }}).then((res)=>{ localStorage.setItem("token",res.data.token) 
    console.log(res.data)
   console.log(localStorage.getItem("token"))
    Navigate(`/${newusername}`)
   
    
  
  } )
  }
function reRenderf(){
  setRerender(!rerender)
}
  return (

    <div className='Wrapper'>

    <img/>

    <PersonIcon/>
 
  
    
    <form onSubmit={updateUser}>
    <img  />
    <input placeholder="name" value={newusername} onChange={e => setnewusername(e.target.value)} / >

    <input placeholder='Bio' />
    <button onClick={reRenderf}  >Render</button>
    <button type='Submit' >Update</button>
    </form>

    </div>

  )
}

export default EditProfilePage

Profile Component:

import axios from "axios"
import { useEffect } from "react"
import { useState } from "react"
import "./Profile.css"
import styled from "styled-components"
import { ProfilePicture } from '../App'
import React from 'react'
import {useContext} from "react"
import { UserNameContext } from '../App'
import PersonIcon from '@mui/icons-material/Person';
import { useNavigate } from 'react-router-dom';
import "./Profile.css"


import { useReducer } from "react"
import EditProfilePage from './EditProfilePage'
const Wrapper = styled.div`
display: flex;
background-color: black;
height: 100vh;

justify-content: center;

`
const Container = styled.div`
background-color: gray;
width: 50%;

`
const Status = styled.div`
display: flex;
font-size: 15px;
`

const H4 = styled.h4`
padding: 10px;

`
const EditProfile = styled.button`

`
const EditProfilePopup = styled.div`
background-color: rgba(0,0,0,0.7);
position: fixed;
height: 50%;


left:40%;
`

    export function Profile (){
      //mAKE SURE ITS NOT UNDEFINED
     const userdata = React.useContext(ProfilePicture)
        const pathname = window.location.pathname
        const[data,setData] = useState([])
        const [user,isUser] = useState(false)
        const[owner,isOwner] = useState(false)
        const[editProfile,seteditProfile] = useState(false)
    
      let username = pathname.split("/")[1]
    
      useEffect(()=>{
        axios.post('http://localhost:5000/profile/getProfile', {
          "username":username
        })
        .then((res)=> {
        
            setData(res.data[0])
            isUser(true)
         
           
        }).catch((err)=> {
           
            isUser(false)
        })
        
      
    
      }, []);
      function checkifOwner(){
        if (userdata.username == username){
         
          isOwner(true)
        }
        else{
       
        
        }
    
      }
      
      setTimeout(checkifOwner,10)
      function editheProfile(){
        seteditProfile(!editProfile)
      }
      return (
    <Wrapper>
    <Container>
    
    
        <div className='Profile'>
        {owner ? <EditProfile onClick={editheProfile} >Edit Profile</EditProfile>:console.log("not Owner") }
        <h2>{data.username}</h2>
        {editProfile ? <EditProfilePopup><EditProfilePage/></EditProfilePopup> : null}
    <img className='ProfileImg' src='https://cdn.pixabay.com/photo/2015/10/05/22/37/blank-profile-picture-973460_1280.png' />
    {user?null:<h2>No User Exists</h2>}
      
        <img  />
        <Status>
    <H4>Followers</H4>
    <H4>Following</H4>
    </Status>
    <H4>Posts</H4>
    
        </div>
       
      
        </Container>
        </Wrapper>
    
      )
    }

I am trying to reRender The Profile Component From My EditProfilePage Component
How can i achieve this?
making it all one component could potentially solve this just wondering if there are any other ways to fix the issue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧瑾黎汐 2025-01-24 11:33:33

将全部组成的组件制成不是一个好方法,最好的方法是使用usememo它将在依赖关系更改时呈现组件,并将新数据发送到组件:例如:

// edit profile.jsx
import Profile from '../componet/Profile'
import {useMemo} from 'react'

export default function EditProfile(){
const [render , setRender] = useState(false);
// every time render changes useMemo will check if render true It will render the component.
const profile = useMemo(() => {
 if(render){
   return <Profile />
 }
} ,[render])

  return(
    <div>
      <button onClick={() => setRender(!render)}>Click To Toggle Render</button>
     {profile}
   </div>
   )
}

Making all in one component is not a good approach the best way is to use useMemo it will render component when dependency changes and also send new data to the component for example:

// edit profile.jsx
import Profile from '../componet/Profile'
import {useMemo} from 'react'

export default function EditProfile(){
const [render , setRender] = useState(false);
// every time render changes useMemo will check if render true It will render the component.
const profile = useMemo(() => {
 if(render){
   return <Profile />
 }
} ,[render])

  return(
    <div>
      <button onClick={() => setRender(!render)}>Click To Toggle Render</button>
     {profile}
   </div>
   )
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文