【问题标题】:Redirecting with state and template literals react使用状态和模板文字进行重定向反应
【发布时间】:2019-12-31 00:08:09
【问题描述】:

输入团队名称后,我希望做出反应,将我们重定向到指定页面(即:“teams/this.state.searchText”,其中搜索文本是用户在搜索表单中输入的内容)。我得到了一个什么都不做/不做重定向的重新渲染......这可以用 reacts new v4 Redirect 组件来完成吗?

export default class Nav extends React.PureComponent {
constructor(props) {
    super(props);
    this.state = {
        searchText: ''
    }
    this.submit = this.submit.bind(this);
}
onSearchChange = e => {
    console.log(e.target.value)
    this.setState({ searchText: e.target.value });
}

submit = e => {
    e.preventDefault();
    // with the new search state from above, get the state and perform a search with it to local/team/"searchValue"

    e.currentTarget.reset();
}
redirectIt = () => {
    this.props.history.push(`teams/${this.state.searchText}`)
}
render() {
    return (
            <Navbar className="bg-light justify-content-between">
                <Form onSubmit={this.submit}  >
                    <FormControl type="text" placeholder="Search Team" className=" mr-sm-2" onChange={this.onSearchChange} />
                    <Button   type="submit">Submit</Button>
                </Form >
                <div className='logo'>NHL</div>
                <Form inline>
                    <Button type="submit" onClick={this.redirectIt}>Login</Button>
                </Form>
            </Navbar>
    );
}

}

【问题讨论】:

标签: javascript html node.js reactjs


【解决方案1】:

使用重定向,它看起来像这样。您基本上可以告诉浏览器转到不同的页面

import { Redirect } from 'react-router-dom'

export default class Nav extends React.Component {

constructor(props) {
    super(props);
    this.state = {
        searchText: '',
        isRedirected: false
    }
}

onSearchChange = e => {
    console.log(e.target.value)
    this.setState({ searchText: e.target.value });
}

submit = e => {
    e.preventDefault();
    // with the new search state from above, get the state and perform a search with it to local/team/"searchValue"

    e.currentTarget.reset();
}

redirectIt = () => {
    this.setState({isRedirected: true})
}

render() {
    // change the to prop to the next component
    if (this.state.isRedirected) return <Redirect to=`/teams/${this.state.searchText}` />

    return (
            <Navbar className="bg-light justify-content-between">
                <Form onSubmit={this.submit}>
                    <FormControl type="text" placeholder="Search Team" className=" mr-sm-2" onChange={this.onSearchChange} />
                    <Button type="submit">Submit</Button>
                </Form >
                <div className='logo'>NHL</div>
                <Button onClick={this.redirectIt}>Login</Button>
            </Navbar>
    );
}

【讨论】:

  • 不起作用。 onClick 仍然没有动作,也没有重新渲染发生
  • 我将 onclick 从按钮向上移动到表单,现在它像第一个表单一样提交,这应该更接近
  • 好吧,再试一次。我将其他一些小东西(例如 PureComponent)更改为 Component。你从我更新的代码 sn-p 得到什么效果
  • 按钮点击仍然没有响应
  • 如果你在函数redirectIt中放了一个控制台日志,它会出现在控制台中吗?
猜你喜欢
  • 2021-11-16
  • 2020-04-22
  • 2020-04-13
  • 1970-01-01
  • 2021-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-28
相关资源
最近更新 更多