react-router createElement和render失败(react-router createElement and render fail)
我正在探索react-router并遇到了一个非常基本的问题。 令人沮丧的部分是我在另一个应用程序中使用react-router,它工作正常!!
我在控制台中收到此错误。 我在这里想念的是什么?
警告:React.createElement:type不应为null,undefined,boolean或number。 它应该是一个字符串(对于DOM元素)或一个ReactClass(对于复合组件)。
未捕获的不变违规:元素类型无效:期望一个字符串(对于内置组件)或一个类/函数(对于复合组件)但得到:object。
var React = require('react'); var ReactDOM = require('react-dom'); var Router = require('react-router'); var Route = require('react-router').Route; var hashHistory = require('react-router').hashHistory; var App = React.createClass({ render: function() { return <div>App Rendered from React Router!</div> } }); //this works - there is nothing wrong with App component // ReactDOM.render(( // <App/> // ), document.getElementById('app')); //This works - just so I understand the basics // var Child = React.createElement('div',{className:'child'}); // var Parent = React.createElement('div',{className:'parent'},Child); // ReactDOM.render(Parent, document.getElementById('app')); //this doesn't work - copy pasted from tutorial ReactDOM.render(( <Router history={hashHistory}> <Route path="/" component={App}/> </Router> ), document.getElementById('app')); //Babel transpiled code // ReactDOM.render(React.createElement( // Router, // { history: hashHistory }, // React.createElement(Route, { path: '/', component: App }) // ), document.getElementById('app')); //this doesn't work // var route = React.createElement(Route, { path: '/', component: App }); // var router = React.createElement(Router,{ history: hashHistory }); // // ReactDOM.render(router, document.getElementById('app'));
I am exploring react-router and ran into a very basic issue. The frustrating part is I am using react-router in another app and it works fine!!.
I get this error in console. What am I missing here?
Warning: React.createElement: type should not be null, undefined, boolean, or number. It should be a string (for DOM elements) or a ReactClass (for composite components).
Uncaught Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.
var React = require('react'); var ReactDOM = require('react-dom'); var Router = require('react-router'); var Route = require('react-router').Route; var hashHistory = require('react-router').hashHistory; var App = React.createClass({ render: function() { return <div>App Rendered from React Router!</div> } }); //this works - there is nothing wrong with App component // ReactDOM.render(( // <App/> // ), document.getElementById('app')); //This works - just so I understand the basics // var Child = React.createElement('div',{className:'child'}); // var Parent = React.createElement('div',{className:'parent'},Child); // ReactDOM.render(Parent, document.getElementById('app')); //this doesn't work - copy pasted from tutorial ReactDOM.render(( <Router history={hashHistory}> <Route path="/" component={App}/> </Router> ), document.getElementById('app')); //Babel transpiled code // ReactDOM.render(React.createElement( // Router, // { history: hashHistory }, // React.createElement(Route, { path: '/', component: App }) // ), document.getElementById('app')); //this doesn't work // var route = React.createElement(Route, { path: '/', component: App }); // var router = React.createElement(Router,{ history: hashHistory }); // // ReactDOM.render(router, document.getElementById('app'));
原文:https://stackoverflow.com/questions/35916055
最满意答案
你可以使用Lookahind和Lookahind。 喜欢这个:
System.out.println(Arrays.toString("a;b;c;d".split("(?<=;)"))); System.out.println(Arrays.toString("a;b;c;d".split("(?=;)"))); System.out.println(Arrays.toString("a;b;c;d".split("((?<=;)|(?=;))")));
你会得到:
[a;, b;, c;, d] [a, ;b, ;c, ;d] [a, ;, b, ;, c, ;, d]
最后一个是你想要的。
((?<=;)|(?=;))
等于之前选择一个空字符;
或之后 。希望这可以帮助。
编辑 Fabian Steeg对可读性的评论是有效的。 可读性一直是RegEx的问题。 有一件事,我帮助缓解这个是创建一个名称代表正则表达式并使用Java String格式来帮助的变量。 喜欢这个:
static public final String WITH_DELIMITER = "((?<=%1$s)|(?=%1$s))"; ... public void someMethod() { ... final String[] aEach = "a;b;c;d".split(String.format(WITH_DELIMITER, ";")); ... } ...
这有一点帮助。 :-D
You can use Lookahead and Lookbehind. Like this:
System.out.println(Arrays.toString("a;b;c;d".split("(?<=;)"))); System.out.println(Arrays.toString("a;b;c;d".split("(?=;)"))); System.out.println(Arrays.toString("a;b;c;d".split("((?<=;)|(?=;))")));
And you will get:
[a;, b;, c;, d] [a, ;b, ;c, ;d] [a, ;, b, ;, c, ;, d]
The last one is what you want.
((?<=;)|(?=;))
equals to select an empty character before;
or after;
.Hope this helps.
EDIT Fabian Steeg comments on Readability is valid. Readability is always the problem for RegEx. One thing, I do to help easing this is to create a variable whose name represent what the regex does and use Java String format to help that. Like this:
static public final String WITH_DELIMITER = "((?<=%1$s)|(?=%1$s))"; ... public void someMethod() { ... final String[] aEach = "a;b;c;d".split(String.format(WITH_DELIMITER, ";")); ... } ...
This helps a little bit. :-D
相关问答
更多-
你可以使用Lookahind和Lookahind。 喜欢这个: System.out.println(Arrays.toString("a;b;c;d".split("(?<=;)"))); System.out.println(Arrays.toString("a;b;c;d".split("(?=;)"))); System.out.println(Arrays.toString("a;b;c;d".split("((?<=;)|(?=;))"))); 你会得到: [a;, b;, c;, d] [a, ...
-
分隔符列表上的Python拆分字符串,也保留分隔符(Python Split String On A List Of Delimiters, Also Preserve Delimiters)[2022-02-20]
使用re.split 。 >>> x = 'if (x==5) {' >>> [i for i in re.split(r'(==)|(\d+)|([(){]|[a-z]+)|\s+', x) if i and i != None ] ['if', '(', 'x', '==', '5', ')', '{'] 捕获组将保持分隔符。 As @dylrei mentioned in the comments, this is lexing. The lexing tool http://www.dabeaz. ... -
如果不是星期六晚上,我不会回答: String s1 = "26+78(12*23)-16"; for(String s: s1.split("(?<=[()+*/-])|(?=[()+*/-])")){ System.out.println(">> " + s); } 得到: >> 26 >> + >> 78 >> ( >> 12 >> * >> 23 >> ) >> - >> 16 I wouldn't answer if it wasn't saturday ...
-
在Java中拆分字符串,但在每个新字符串中保留分隔符(Split a string in Java, but keep the delimiters inside each new string)[2023-11-12]
使用Pattern和regex解决方案: 我会单独捕捉每个单词,或者在String之前和之后使用一个元素的单词 String str = "word1{word2}[word3](word4)\"word5\"'word6'"; Matcher m = Pattern.compile("(([{\\[(\"']\\w+[}\\])\"'])|(\\w+))").matcher(str); Listmatches = new ArrayList<>(); while (m.find()) ... -
看起来你想分开 在- . _ 或数字和字母之间的字符。 在这种情况下,你可以尝试 yourText.split("[-._]|(?<=[0-9])(?=[a-zA-Z])") It looks like you want to split on - . _ or between digit and alphabetic characters. In that case you can try with yourText.split("[-._]|(?<=[0-9])(?=[a-zA-Z])")
-
对于大多数正则表达式,您可以通过将拆分表达式放在捕获组中来做一些事情。 new Regex("(<[^>]+>)").Split("test string") 生成带有项目的数组 test string (最后是一个空字符串) 如果你想找到标签内的东西,并且它们肯定没有以任何方式嵌套,你就可以做到 new Regex("(<[^>]+>[^<]*[^>]+>)").Split("test string") = test string ...
-
如何拆分字符串并使用boost :: split保留分隔符?(How to split a string and keep the delimiters using boost::split?)[2022-05-29]
你不能使用boost::split因为使用boost/algorithm/string/find_iterator.hpp的split_iterator的内部实现吞下了令牌。 但是你可以使用boost::tokenizer ,因为它有一个保留分隔符的选项: 每当在输入序列中看到分隔符时,当前令牌就完成了,并且新的令牌开始。 dropped_delims中的分隔符不会在输出中显示为令牌,而keep_delims中的分隔符确实显示为令牌。 http://www.boost.org/doc/libs/1_55_0 ... -
在字符串被拆分后保留分隔符“ - ”(“ - ”)[重复](keep delimiter “-” with after the string is split(“-”) [duplicate])[2022-05-16]
你可以拆分空间: strArr = word.split(" "); You can split on the space : strArr = word.split(" "); -
没有regexp,虽然我不确定它是更快还是更慢: vector
split(string& stringToSplit) { vector result; size_t pos = 0, lastPos = 0; while ((pos = stringToSplit.find_first_of(";,|", lastPos)) != string::npos) { result.push_back(stringToSplit. ... -
如何在多个分隔符上拆分字符串以保留一些分隔符?(How to split string on multiple delimiters keeping some delimiters?)[2022-01-25]
试试这个: $src='word1&word2 !word3 word4 &word5'; $arr=explode(' ',$src=preg_replace('/(?<=[\w])([&!])/',' $1',$src)); echo join('
',$arr); // present the result ... 首先将由单个字符[&!]组成的组的任何出现更改为' $1' (=本身,前面带有空格),然后使用该字符串explode()空白作为分隔符。 如果您需要处理多个空格作为单词之间的分隔符 ...