andyChenAn / frontEnd

前端知识积累(blog)
Apache License 2.0
13 stars 1 forks source link

Prompt组件 #20

Open andyChenAn opened 5 years ago

andyChenAn commented 5 years ago

react路由组件之Prompt组件

Prompt组件主要用于在用户离开页面之前提醒用户。当

Prompt组件有以下几个属性:

class Form extends Component {
    constructor (props) {
        super(props);
        this.state = {
            isBlocking : false
        }
    }
    render () {
        const {isBlocking} = this.state;
        return (
            <form onSubmit={event => {
                event.preventDefault();
                event.target.reset();
                this.setState({
                    isBlocking : false
                })
            }}>
                <Prompt when={isBlocking} message={location => {
                    return `Are you sure you want to go to ${location.pathname}`;
                }} />
                <p>Blocking? {isBlocking ? "yes , click a link ore the back button" : 'nope'}</p>
                <div>
                    <input type="text" placeholder="type something to block transitions" onChange={(event) => {
                        let value = event.target.value;
                        this.setState({
                            isBlocking : value.length > 0
                        });
                    }} />
                </div>
                <button>Submit to stop blocking</button>
            </form>
        )
    }
}

class App extends Component {
    render () {
        return (
            <Router>
                <div>
                    <ul>
                        <li>
                            <Link to="/">Form</Link>
                        </li>
                        <li>
                            <Link to="/one">One</Link>
                        </li>
                        <li>
                            <Link to="two">Two</Link>
                        </li>
                    </ul>
                    <Route path="/" exact component={Form} />
                    <Route path="/one" render={() => <h1>one</h1>} />
                    <Route path="/two" render={() => <h1>two</h1>} />
                </div>
            </Router>
        )
    }
}

上面代码就是一个简单的Prompt组件使用的例子,当我们在输入框输入东西时,然后点击链接,就会渲染Prompt组件,如果点击确定,那么会跳转,如果点击取消那么就不会跳转。

yangfir commented 3 years ago

Router组件传getUserConfirmationprops来自定义拦截


const getConfirmation = (message, callback) => {
    Modal.confirm({
      title: message,
      onCancel: () => {
        callback(false);
      },
      onOk: () => {
        callback(true);
      }
    });
}

<BrowserRouter getUserConfirmation={getConfirmation} >
        <App />
 </BrowserRouter>
andyChenAn commented 3 years ago

s来自定义拦截

多谢提醒