简体   繁体   English

单击指向同一页面的链接时更改个人资料图片

[英]change profile picture when click in link to the same page

I have an user profile (/someuser) that I open using:我有一个用户配置文件 (/someuser),我使用以下方式打开:

<Route path="/:user" component={Profile} />

The profile is like this:简介是这样的:

export default function App(props) {

const [nick, setNick] = useState(props.match.params.user); //get the nick using props.
const [profile, setProfile_picture] = useState(profile_picture); // default one

useEffect(() => {

// get current user profile picture:
firebase.database().ref('/users/').orderByChild('user').equalTo(props.match.params.user).once('value').then(snapshot => {
        if (snapshot.exists()){
            var img = snapshot.val().img;
            setProfile_picture(img);
        }
});
}, []);

return(
    <>
<img className={classes.profile} src={profile} />
<h1>{nick}</h1>
<Link to="/user2">Go to profile 2</Link>
</>
);
}
);

Supose I'm in /user profile and I click in the Link at the end of the code to go to the /user2.假设我在 /user 配置文件中,然后单击代码末尾的链接到 go 到 /user2。 What I'd like to do is change the profile picture and the nick (without reload).我想做的是更改个人资料图片和昵称(无需重新加载)。 Force after click to update this informations... Any ideas how can I do this?单击后强制更新此信息...任何想法我该怎么做?

Thanks.谢谢。

You need to trigger the useEffect based on params change because when the params change component is re-rendered by react-router and hence the state will not be re-initialized.您需要根据参数更改触发useEffect ,因为当参数更改组件由 react-router 重新渲染时,state 将不会重新初始化。

Triggering the useEffect on params change will ensure data is updated when params change.在参数更改时触发 useEffect 将确保在参数更改时更新数据。

Also make sure to set nick state too ni useEffect as that too needs to update on params change还要确保set nick state too ni useEffect 因为这也需要更新参数更改

useEffect(() => {

    // set nick state
    setNick(props.match.params.user);
    // get current user profile picture:
    firebase.database().ref('/users/').orderByChild('user').equalTo(props.match.params.user).once('value').then(snapshot => {
            if (snapshot.exists()){
                var img = snapshot.val().img;
                setProfile_picture(img);
            }
    });
}, [props.match.params.user]);

Do you mean without reloading the page?你的意思是不重新加载页面? If so then I think you can make a boolean that toggles when you go to a different route and initiates then useEffect hook by putting it in the dependency array.如果是这样,那么我认为您可以制作一个 boolean ,当您将 go 切换到不同的路线并启动然后 useEffect 挂钩时,将其放入依赖项数组中。 Or you could move the code to a separate component and pass it in as a prop.或者您可以将代码移动到单独的组件中并将其作为道具传递。 Now to do it completely without redirecting the browser would be to use the toggle boolean as mentioned before, but using fetch api, axios, etc to send that nick to a backend and change it accordingly which I think is similar to an Ajax request I hope this helps. Now to do it completely without redirecting the browser would be to use the toggle boolean as mentioned before, but using fetch api, axios, etc to send that nick to a backend and change it accordingly which I think is similar to an Ajax request I hope这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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