jsx

玩转 React(三)- JavaScript代码里写HTML一样可以很优雅

▼魔方 西西 提交于 2020-01-18 00:11:57
这是《玩转 React》系列的第三篇,看到本篇的标题,了解过 React 的同学可能已经大致猜到我要讲什么了,本篇中要讲的内容对于刚接触 React 的同学来说,可能有些难以接受,但希望你能坚持学下去,这是 Facebook 的前端大神们为前端开发做出的革命性创新。 React 第一印象 废话不多说,先看一段代码: class HelloMessage extends React.Component { render() { return <div>Hello {this.props.name}</div>; } } ReactDOM.render(<HelloMessage name="John" />, mountNode); 这是从 React 官网首页粘贴过来的一段示例代码,简单解释一下,这段代码实现了一个名为 HelloMessage 的组件,它接收一个 name 属性,可以在页面上展示出 Hello xxx。 ReactDOM.render 是用来将某个组件渲染到页面的某个 DOM 节点上。 在之后的文章中,我们会详细讲解如何创建 React 组件以及如何开发一个完整的 React 项目。现在,我更想跟大家探讨的是,你看了上述这段代码,算是对 React 有了第一印象,内心是怎样的感受? 我相信,很多人第一次看到这样的代码时的感受都是:“我擦,这是什么玩意儿

How to pass an array as props in React without being converted to string

末鹿安然 提交于 2020-01-16 16:48:56
问题 I'm developing a university administration website with React that can be displayed in different languages. So far, I have developed the Login page, please see the code (note that I'm using Babel ): import React from 'react' import PropTypes from 'prop-types' import { fetchPopularRepos } from '../utils/api' function LanguagesNav ({ selected, onUpdateLanguage}) { const languages = ['EU', 'ES', 'EN'] return ( <div > <h1 className='center-text header-lg'> GAUR 2.0 </h1> <ul className='flex

curly braces in es6 arrow function for each [duplicate]

为君一笑 提交于 2020-01-16 16:47:09
问题 This question already has answers here : Arrow function without curly braces (4 answers) Closed 2 years ago . We create presentational component or stateless component like this const MyComponent = () => { return(<div>my component</div>) } but I'd seen this const MyComponent = () => <div> <h1>head</h1> my component </div> so now I'm confused when the braces is needed when using es6's arrow function. This confused me on when rendering a list using map shorter version <div> {map(o => <div>{o

react-jsx语法

守給你的承諾、 提交于 2020-01-16 16:04:11
JSX语法 如要要使用 JSX 语法,必须先运行 cnpm i babel-preset-react -D ,然后再 .babelrc 中添加 语法配置; babelrc配置 { "presets": ["env", "stage-0", "react"], "plugins": ["transform-runtime"] } webpack中配置 module: { rules: [ { test: /\.css$/, use: ['style-loader', 'css-loader?modules&localIdentName=[name]_[local]-[hash:5]'] }, // 通过 为 css-loader 添加 modules 参数,启用 CSS 的模块化 { test: /\.scss$/, use: ['style-loader', 'css-loader', 'sass-loader'] }, { test: /\.(png|gif|bmp|jpg)$/, use: 'url-loader?limit=5000' }, { test: /\.jsx?$/, use: 'babel-loader', exclude: /node_modules/ } ] } JSX语法的本质:还是以 React.createElement 的形式来实现的,并没有直接把

React Js setState with params when onClick

心已入冬 提交于 2020-01-15 08:59:33
问题 I am using Reactjs to make a simple list of albums and after a click on each item I have to get all photos of this album, so I think to change the state (album_Id) .. the problem is I can't change the state and I dont know how to load the second page (to display all photo of one album) independently. please I need your help. this is my code (App.js): import React, { Component } from 'react'; import { BrowserRouter as Router, Route, Link } from 'react-router-dom'; import Photolist from '.

Cant find react error which child is missing key prop

强颜欢笑 提交于 2020-01-15 05:40:42
问题 I get the very common Warning: Each child in an array or iterator should have a unique "key" prop. This is usually very easy to resolve but in this case its just impossible to me to figure out where the issue is created. My stack trace: in hoc (created by Connect(hoc)) in Connect(hoc) (at withTranslation.js:7) in hoc (at ConnectedProtectedRoutes.js:26) in Route (at ConnectedProtectedRoutes.js:44) in ProtectedRoutes (created by Connect(ProtectedRoutes)) withTranslation Component export

React.js call recursive function in render

萝らか妹 提交于 2020-01-13 20:29:09
问题 Trying to display a hierarquical tree view with React.js My render method: render: function(){ var self= this; return( <div className="panel panel-flat content-group-lg"> <div className='panel-body'> <div className="tree"> <ul> <li> <a href="#">Categorias</a> { self.printTree(this.state.tree_array) } </li> </ul> </div> </div> </div> ) } and my printTree method: printTree: function(level){ console.log(level); var self = this; var level_length = level.length; if(level_length >= 1){ return( <ul>

React.js call recursive function in render

血红的双手。 提交于 2020-01-13 20:29:03
问题 Trying to display a hierarquical tree view with React.js My render method: render: function(){ var self= this; return( <div className="panel panel-flat content-group-lg"> <div className='panel-body'> <div className="tree"> <ul> <li> <a href="#">Categorias</a> { self.printTree(this.state.tree_array) } </li> </ul> </div> </div> </div> ) } and my printTree method: printTree: function(level){ console.log(level); var self = this; var level_length = level.length; if(level_length >= 1){ return( <ul>

ternary operator inside map

自古美人都是妖i 提交于 2020-01-13 19:17:50
问题 I'm working on a webapp where the user views a list of items and can mark certain items as favourites. The favourites are provided as an array of ID's with a prop (this.props.favourites). To prevent duplicate entries I want to replace the 'add to favourites' button with a 'delete from favourites' button if the item.id is already in the this.props.favourites array. However, the ternary If statement I've made crashes on a syntax error. Yet I thought I followed the official example (https:/