jsx

babel 编译后 this 变成了 undefined

谁都会走 提交于 2020-01-07 05:57:42
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 最近有在用webpack,使用了babel这个模块来编译js jsx文件,但是发现文件编译后this变成了undefined。 源文件 module.exports = React.createClass({ render: () => { return (<div>{this.props.name}</div>); } }); 编译后 module.exports = React.createClass({ render: () => { return (<div>{undefined.props.name}</div>); } }); 这个原因是因为webpack规则中babel用了es2015的编译规则 { test: /\.jsx?$/, loader: "babel", query: { presets: ['react', 'es2015'] } } 解决的方法是这样修改 module.exports = React.createClass({ render: function(){ return (<div>{this.props.name}</div>); } }); 其根本的原因是es2015的箭头函数,这个一个不能忽视的问题 普通function函数和箭头函数的行为有一个微妙的区别

When to use anonymous functions in JSX

主宰稳场 提交于 2020-01-06 21:13:26
问题 can someone explain the difference between an anonymous function <button onClick={() => this.functionNameHere()}></button> and calling the function like below <button onClick={this.functionNameHere()}></button> and also when to use either (such as different scenarios to use one over the other). 回答1: In ES6 , with the first scenario "this" refers to the Component where the function called belongs to. <button onClick={() => this.functionNameHere()}></button> is equivalent to <button onClick=

How to console.log in React application using JSX ?

送分小仙女□ 提交于 2020-01-06 18:44:01
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> In this tutorial, we are going to discuss how to perform console.log() in react and react native application inside the render block. Most of the times developer trying to add console.log statement inside the return block of render block, in that case it will not print the statement inside the console rather it will print the results inside the browser screen. Lets see the below example, where we are using console.log statement inside the return block of render function. Wrong Method render() { return ( <div> <h1>List of countries</h1> console.log(this.props

对Taro Next小程序跨框架开发的探索与实践-----------------引用

北城以北 提交于 2020-01-06 14:43:47
介绍 Taro 如何将各种语法的前端框架(如:React/Vue等)运行在小程序上,讨论一个框架支持多 DSL 的实现探索,使得开发者可以使用任意热门框架/语法/DSL 来编写小程序应用,同时复用相关生态。 各大小程序开发框架也纷纷进行了多端适配 微信小程序主要分为 逻辑层 和 视图层,以及在他们之下的原生部分。逻辑层主要负责 JS 运行,视图层主要负责页面的渲染,它们之间主要通过 Event 和 Data 进行通信,同时通过 JSBridge 调用原生的 API。这也是以微信小程序为首的大多数小程序的架构。 Taro 当前架构 Taro 当前的架构主要分为:编译时 和 运行时。 其中编译时主要是将 Taro 代码通过 Babel 转换成 小程序的代码,如:JS、WXML、WXSS、JSON。运行时主要是进行一些:生命周期、事件、data 等部分的处理和对接。 Taro 编译时 有过 Babel 插件开发经验的应该对一下流程十分熟悉,Taro 的编译时也是遵循了此流程,使用 babel-parser 将 Taro 代码解析成抽象语法树,然后通过 babel-types 对抽象语法树进行一系列修改、转换操作,最后再通过 babel-generate 生成对应的目标代码。 整个编译时最复杂的部分在于 JSX 编译。 我们都知道 JSX 是一个 JavaScript 的语法扩展

ReactJS need to conditionally display different onclick vs Link based on logic

*爱你&永不变心* 提交于 2020-01-06 06:59:54
问题 Goal I have it to display different HTML with onclick vs. route Without logic in the Render() const contents = this.state.data.map(item => ( This is the logic I'm struggling with <button id={item.Member_ID} type="button" {` ${this.isExist(item.Member_ID) ? <Link to='surveysai/'> <button type="button" className='btn btn-success'>SAI</button> </Link> : ${onClick={(e) => this.downloadUser(item.Member_ID, e)}}`} className={`btn ${this.isExist(item.Member_ID) ? "btn-success" : "btn-warning"}`} >

How can I filter json data from api call in react into different sections of the component

倖福魔咒の 提交于 2020-01-05 04:10:09
问题 I have 2 json objects that contain sales period information based on Month to data and year to date. This data includes Units Sold, Gross Revenue, Year to Date, TOTALS, Month to Date, DAILY AVGs,Expenses, Net Revenues, and PER UNIT. I do not want to map this by creating a loop. I want to take the columns out that I need and place them in different segments In the React Semantic UI code Below A big part of the reason for me not wanting to map it and do a loop is because I also want to reuse

Webpack2, how to exclude react and react dom from build

对着背影说爱祢 提交于 2020-01-04 23:43:09
问题 My goal is to create a module for a react components library made in ES6 using webpack. I am using: webpack: "2.1.0-beta.25" react: "15.4.1" I need to assign react and react-dom as peerDependencies so it doesn't get downloaded by the developer. This is the relevant part of my package.json : "peerDependencies": { "react": "15.4.1" }, "dependencies": { "chalk": "1.1.3", "compression": "1.6.2", "cross-env": "3.1.3", "immutable": "3.8.1", "invariant": "2.2.1", "lodash": "4.16.4", "minimist": "1.2

ReactJS: unexpected token '<'

帅比萌擦擦* 提交于 2020-01-04 14:33:10
问题 Hello I tried to search in other questions but none of mentioned solutions I tried did not work for me. When using command: npm start I have an error: ERROR in ./src/index.js Module build failed (from ./node_modules/babel-loader/lib/index.js): SyntaxError: D:/Kodilla/Projekty/webpack-to-do-app/src/index.js: Unexpected > token (6:4) 5 | ReactDOM.render( 6 | <App />, | ^ 7 | document.getElementById('app') 8 | ); Defined command in package.json: "scripts": { "test": "echo \"Error: no test

Comment JSX code in Sublime and atom

孤街浪徒 提交于 2020-01-04 03:41:49
问题 Is there any way how to comment a code inside return statement in render function in React.js (JSX) without curly brackets? It is pretty annoying to write curly brackets everytime you want to comment some piece of code. 回答1: If you feel like you need to put too many comments, just put the block, you want to comment in external Component. 回答2: In sublime you can setting up default comments in the following way Create an XML file in Packages/User called Comments.tmPreferences with the following

Is there a tool to convert React Elements javascript code to JSX [closed]

杀马特。学长 韩版系。学妹 提交于 2020-01-04 02:04:04
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 3 years ago . It's not difficult to find a tool to convert JSX to React.createElement , something like this: <div className="commentBox"> Hello, world! I am a CommentBox. </div> to this: React.createElement('div', {className: "commentBox"}, "Hello, world! I am a CommentBox." ) I can't find a tool co make a convertation vice