首先,我是Ract的React的初学者,并尝试着解决问题。我目前正在使用路由,我不知道我哪里出错了。请指导我。
我有一个包含UserWorld.jsx文件的容器文件夹。其内容如下:
import React, { PropTypes } from 'react';
import User from '../containers/User';
// Simple example of a React "smart" component
export default class UserWorld extends React.Component {
constructor(props, context) {
super(props, context);
// How to set initial state in ES6 class syntax
// https://facebook.github.io/react/docs/reusable-components.html#es6-classes
this.state = { users: this.props.data
};
}
render() {
var userNodes = this.state.users.map(function(user) {
return (
<User key={user.id} first_name={user.first_name} last_name={user.last_name} email={user.email} city={user.city} address={user.address} />
);
});
return (
<table>
<thead>
<tr>
<td>First Name</td>
<td>Last Name</td>
<td>Email</td>
<td>Address</td>
<td>City</td>
</tr>
</thead>
<tbody>
{userNodes}
</tbody>
</table>
);
}
}
现在,我想添加一个链接,我包括
import { Router, Route, Link, browserHistory, IndexRoute } from 'react-router'
然而,这在控制台中给出了一个错误,如下所示:
Uncaught Error: Cannot find module "react"
Exception in rendering!
message: Could not find component registered with name UserWorldApp. Registered component names include [ HelloWorldApp ]. Maybe you forgot to register the component?
现在我不清楚这里发生了什么。如果有人可以帮助我,我将不得不承担责任。此外,如果我可以获得相同的参考。我遇到了各种各样的语法问题。
更新
我发现错误出现在node_modules文件夹的react-router文件夹中的Router.js的var _react = require('react');
行中。
我在控制台中得到了这个:
ERROR in ../~/react-router/lib/Router.js
18:03:09 client.1 | Module not found: Error: Cannot resolve module 'imports' in /Users/c193/Documents/React/test-react-on-rails/node_modules/react-router/lib
18:03:09 client.1 | @ ../~/react-router/lib/Router.js 19:13-29
P.S:我读到的所有文档和文章对我来说都太技术化了,我只能理解它的一点一滴。我刚刚开始已经两周了。
答案 0 :(得分:0)
我不知道问题是什么或我错过了什么。但是,在一个全新的项目中,我运行了npm install react-router --save
。我把import { Link } from 'react-router/lib/Link';
放在我的反应组件中。重新启动服务器,我很高兴。我在之前的应用程序中执行了相同的步骤,但它在某种程度上对我不起作用。无论如何,我不知道我错过了什么,但问题现在已经解决了。
无论如何,谢谢你。 :)
答案 1 :(得分:0)
我有类似的问题,但是使用“react-router”而不是“反应”。检查package.json
目录中的client
,如果缺少任何内容,请手动添加并从该目录运行npm install
。这解决了我的问题,虽然有人评论说,你可能还有其他一些问题。
我不认为从根项目目录运行npm install react-router --save
是一个好主意,因为React on Rails使用yarn,所以主项目文件夹中node_modules
目录中唯一的东西应该是一个.yarn.integrity
文件。 React所需的一切都应该放在package.json
文件夹中的node_modules
文件和client
目录中,因此请检查那里是否缺少某些内容。
以下是教程中client/package.json
的{{1}}内容:https://github.com/shakacode/react-webpack-rails-tutorial/blob/master/client/package.json