【问题标题】:Render card details on another page when clicking specific card单击特定卡片时在另一个页面上呈现卡片详细信息
【发布时间】:2019-11-25 18:51:58
【问题描述】:

卡片来自 JSON 文件。代码来自一个名为 painting.js 的文件。卡片都正确呈现,点击后它们将我带到空白的paintingInfo.js 页面。我的问题是我应该在这个新的 paintingInfo.js 页面中包含什么,以便它呈现我存储在本地存储中的卡。 React 相对较新,因此非常感谢任何帮助。基本上,我如何访问paintingInfo.js 页面中的本地存储以进行渲染?

  state = {
    cardInfo: [...cardInfo]
  };

  goToPaintingInfo = (cardInfo) => {
    localStorage.setItem("selectedPainting", cardInfo);
    this.props.history.push("/paintingInfo/:id");
  }

  render() {
    return (
      <React.Fragment>
        <Navbar className="navCustom d-flex justify-space-between" bg="light" variant="light">
          <Navbar.Brand href="/">SNR Arts</Navbar.Brand>
          <Nav className="ml-auto navCust">
            <Nav.Link href="/">Home</Nav.Link>
            <Nav.Link href="/paintings">Paintings</Nav.Link>
            <Nav.Link href="/contact">Contact</Nav.Link>
          </Nav>
        </Navbar>
        <div className="container-fluid">
          <div className="row align-items-center justify-content-between">
            {/* print out cards here */}

            {this.state.cardInfo.map(card => {
              return (
                <div className="col-12 col-sm-3 col-md-2 my-3" key={card.id}>
                  <img
                    src={card.image}
                    alt={card.name}
                    className="img-fluid img-thumbnail rounded indvCard bg-dark"
                    onClick = {()=>this.goToPaintingInfo(card.id)}
                  />
                </div>
              );
            })}
          </div>
        </div>

【问题讨论】:

    标签: javascript reactjs react-router local-storage react-router-dom


    【解决方案1】:

    点击卡片时,您只需发送card,而不是card.id

    onClick = {()=>this.goToPaintingInfo(card)}
    
    goToPaintingInfo = (cardInfo) => {
        localStorage.setItem("selectedPainting", JSON.stringify(cardInfo)); //store complete card
        this.props.history.push(`/paintingInfo/${cardInfo.id}`); //For this you must have Route to handle this request
    }
    

    在路线的某个地方,你必须有

    <Route path="/paintingInfo/:id" exact component={paintingInfo} /> //Write appropriate component name
    

    paintingInfo.js文件

    state={
       card: JSON.parse(localStorage.getItem("selectedPainting"))
    }
    
    render(){
       return(
         <div>
            <img src={this.state.card.image}
                 alt={this.state.card.name}
                 className="img-fluid img-thumbnail rounded indvCard bg-dark"
            />
         </div>
       )
    }
    

    注意:您只能使用react-router-dom 包中的Redirect,而不是this.props.history.push

    import {Redirect} from 'react-router-dom'
    
    goToPaintingInfo = (cardInfo) => {
        localStorage.setItem("selectedPainting", cardInfo); //store complete card
        return <Redirect to={`/paintingInfo/${cardInfo.id}`} />; //For this you must have Route to handle this request
    } 
    

    【讨论】:

    • 在返回前尝试在PaintingInfo.js中打印card,看看是否有值存储在localstorage中,
    • 如果你的路径是"./images/{name of file}",那么你必须使用require,比如`src={require(this.state.card.image)}`。查看更新的答案。
    • 当你这样做时localStorage.setItem("selectedPainting", cardInfo); 然后这样做const item = localStorage.getItem("selectedPainting"); 并检查if(item){this.props.history.push(/paintingInfo/${cardInfo.id});}
    • 或者另一个技巧是你可以使用一些延迟,如setTimeout({this.props.history.push(/paintingInfo/${cardInfo.id});},5000),将在 5 秒后重定向。
    • 非常感谢你!终于解决了这个问题,如果没有你,我无法做到我非常感谢:)!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-31
    • 1970-01-01
    • 2018-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多