elaich
elaich

Reputation: 941

Going to another route must unmount current route component

Here is my use case, I made a simple example in order to ask this question, let's say I have two routes, /view1 and /view2, each mounting a component, View1 and View2, I need View1 to unmount when I go to /view2.

Here is the code if you want to try:

import React from 'react';
import ReactDOM from 'react-dom';
import {Router, Route, Link, browserHistory} from 'react-router';
import Portal from 'react-portal';

class View1 extends React.Component {
    constructor(context, props) {
        super(context, props)
    }

    componentDidMount() {
        console.log('view 1 mounted');
    }

    componentWillUnmout() {
        console.log('view 1 unmount');
    }

    render() {
        return (
            <h2>hanta</h2>
        );
    }
}

class View2 extends React.Component {
    constructor(context, props) {
        super(context, props)
    }

    componentDidMount() {
        console.log('view 2 mounted');
    }

    componentWillUnmout() {
        console.log('view 2 unmount');
    }

    render() {
        return (
            <h2>View 2</h2>
        );
    }
}

class View extends React.Component {
    render() {
        return (
            <div>
                <h1>View</h1>
                <Link to="/view1">view 1</Link>
                <Link to="/view2">view 2</Link>
                {this.props.children}
            </div>
        );
    }
}

ReactDOM.render(
    <Router history={browserHistory}>
        <Route path="/" component={View}>
            <Route path="/view1" component={View1} />
            <Route path="/view2" component={View2} />
        </Route>
    </Router> 
    , document.getElementById("container"))

ComponentWillUnmout doesn't get called in any case, I'm using:

├── [email protected]

├── [email protected]

├── [email protected]

Upvotes: 1

Views: 1159

Answers (1)

taion
taion

Reputation: 2837

You spelled it wrong. It should be componentWillUnmount, not componentWillUnmout.

Upvotes: 1

Related Questions