简体   繁体   English

反应不更新DOM(Laravel)

[英]React not updating the DOM (Laravel)

I have set up a basic react app with hash navigation. 我已经设置了一个带哈希导航的基本反应应用程序。 Problem is when I click on any link to navigate between pages, I see that the hash in the url is changing properly, as well as I added a console.log in my layour's render to see if it's getting called and it is, with proper this.props.children values, however the page is not rendering anything. 问题是,当我点击任何链接在页面之间导航时,我看到网址中的哈希值正在改变,我在我的layour的render添加了一个console.log来查看它是否被调用,并且它是正确的this.props.children值,但页面不呈现任何内容。 If I go to any route and refresh the page I see the correct components rendered, but if I navigate somewhere from there noting gets rendered until I refresh again. 如果我去任何路线并刷新页面,我会看到渲染的正确组件,但是如果我从那里导航到某处,则会在我再次刷新之前进行渲染。

import React from "react";
import ReactDOM from "react-dom";
import { IndexRoute, Router, Route, Link, hashHistory as history } from 'react-router';

class Layout extends React.Component {

  render() {
    console.log(this.props, document.location.hash);

    return <div>
      <div>
        <span>LEYAUTI MLEAYTI {Math.random()}</span>
      </div>
      <div>
        {this.props.children}
      </div>
      <div>
        {this.props.params.project}
      </div>
    </div>
  }
}

class CreateProject extends React.Component {

  render() {
    return <div>
      <h1>Create PROEKT</h1>
    </div>
  }
}

class Projects extends React.Component {

  render() {
    return <div>
      <h1>PROEKTI MROEKTI</h1>
      <Link to="/projects/create">New project</Link>
    </div>
  }
}

ReactDOM.render(
    <Router history={history}>
      <Route path="/" component={Layout}>
        <IndexRoute component={Projects}/>
        <Route path="projects/create" component={CreateProject}/>
      </Route>
    </Router>,
    document.getElementById('app-root'));

Here is a visual of what's happening in the console when I navigate on a couple routes, but the DOM remains unchanged 下面是我在几条路线上导航时控制台中发生的事情的视觉效果,但DOM保持不变

在此输入图像描述

This may be an issue with hashHistory. 这可能是hashHistory的问题。 Which react-router version are you using? 您使用哪个react-router版本? With v4 and above, you need to use history like so - 使用v4及更高版本,您需要使用这样的历史记录 -

import createHistory from 'history/createHashHistory'
const history = createHistory()
// pass history to the Router....

Your component didn't actually unmount/remount if you only update your hashtag in your url. 如果您只更新网址中的主题标签,则您的组件实际上并未卸载/重新安装。 The route however, is updated. 然而,路线已更新。 So you can only see the component loads content for once when you refresh the page. 因此,您只能在刷新页面时看到组件加载内容一次。

You will need to create state variables and update it in a routeChange handler callback and bind the updated state variable to your view by using setState. 您需要创建状态变量并在routeChange处理程序回调中更新它,并使用setState将更新的状态变量绑定到您的视图。 Then the component can get updated. 然后组件可以更新。

See this post for how to add the route change listener ( https://github.com/ReactTraining/react-router/issues/3554 ) 有关如何添加路由更改侦听器的信息,请参阅此文章( https://github.com/ReactTraining/react-router/issues/3554

Alright, so I got down to the bottom of it. 好吧,所以我深究了它。

The problem was that I was including my client.min.js file before the default app.js file of laravel 5.4's default layout. 问题是我在laravel 5.4的默认布局的默认app.js文件之前包含了我的client.min.js文件。 For some reason it broke react in a very weird way. 由于某种原因,它以非常奇怪的方式破坏了反应。 What I had to do is switch the order in which the two files were included. 我要做的是切换包含两个文件的顺序。

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

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