jsx

react路由解决方案

纵然是瞬间 提交于 2019-12-04 05:52:36
react-router:是react router的核心库 react-router-dom:是网页路由的解决方案,依赖于react-router react-router-native:是react-native解决移动端路由的解决方案,依赖于react-react 下面讲述的是react-router-dom的使用 这里用的react-router版本是5.x react-router5.x版本和react-router3.x使用上有很大不同 1安装路由模块 npm i –S react-router-dom 2路由的定义 App.jsx 这里定义了4个页面 <Router>定义一个路由实例,相当于一个路由容器 <Switch>标签让路由只能匹配单个<Route>,不具包含性的匹配多个路由 5.x的路由具有包含性和3.x的路由完全不同 exact属性让route进行严格匹配,只有路径完全一致才会匹配成功,默认值true,可以省略不写 eq: 在网页中匹配根路径’/’,路由匹配会按从上到下的顺序进行匹配,如果没有<Swith>标签,就会成功匹配’/’,’/about’,’/inbox’,’/coder’所有的<Route>标签,从而展示其相关的组件。 添加<Swith>标签后,路由只能匹配单个<Route>标签,因此这里只会匹配到path=’/’的<Route>标签

create react app not loading css background images in dev or build

旧时模样 提交于 2019-12-04 04:29:00
I'm having difficulty with some background images not loading. I've made some significant modifications to the initial create react app, my folder structure is now as follows: Note: I have omitted some files & folders, if you need more please let me know. App/ node_modules/ src/ client/ build/ node_modules/ public/ src/ css/ App.css images/ tree.png yeti.png App.jsx server/ package.json Procfile Here are the steps I take to create this issue: $ cd src/server && npm run dev This will boot up the dev server and open the browser to my app, everything is working fine except for some elements on

JSX doesn't evaluate integer in expression as boolean

依然范特西╮ 提交于 2019-12-04 04:13:43
问题 I'm used to write render optional Components like this: var Foo = React.createClass({ render: function() { var length = 0; return <div>Foo {length && <Bar />}</div>; } }); This shorthand if is mentioned in the if/else in JSX guide as immediately-invoked function expression. However, since my latest update of React, it started to render 0 instead of null . Here is a jsfiddle Why is that happening? 回答1: The && operator evaluates the left-hand expression first, and if the left-hand expression

Returning multiple elements in JSX

不想你离开。 提交于 2019-12-04 03:22:26
问题 I'm trying to return two links if the user is not logged in. Something like this: <Nav> {if(this.state.user) { <NavItem onClick={this.logout}>Log out</NavItem> } else { <NavItem onClick={this.login}>Log in</NavItem> <NavItem onClick={this.register}>Register</NavItem> } } </Nav> I know I can do this with a ternary operator: <Nav> {this.state.user ? <NavItem onClick={this.logout}>Log out</NavItem> : <NavItem onClick={this.login}>Log in</NavItem> } </Nav> The problem is I want to render the two

React - Create nested components with loops

回眸只為那壹抹淺笑 提交于 2019-12-04 03:13:28
I have a little issue with React. I can't create a nested component with a for loop. What I want to do is create 9 cells of a table and then create 3 rows with 3 cells for every row and after that mount the 3 rows together and create a board 9x9. Let say that I want to get something like this, but using a loop class Board extends React.Component { renderSquare(i) { return <Square value={this.props.squares[i]} onClick={() => this.props.onClick(i)} />; } render(){ return( <div> <div className="board-row"> {this.renderSquare(0)} {this.renderSquare(1)} {this.renderSquare(2)} </div> <div className=

Delay array map iteration in React

有些话、适合烂在心里 提交于 2019-12-04 01:44:56
问题 I have this array, that I want to iterate. I need to delay it by a couple of seconds before the next. {this.props.things.map((thing, index) => { return ( <div key={index}>{thing.content}</div> // Delay 1 second here ) })} The initial state of this array is always more than one. For UI purposes I want them to load in one by one in to the DOM. 回答1: The render function of react is synchronous. Also javascript map is synchronous. So using timers is not the right solution here. You can however, in

React, how to access the DOM element in my render function from the same component

假如想象 提交于 2019-12-03 23:44:10
I'm wondering what's the best practice for accessing the DOM elements inside my render function from the same component. Note that I will be rendering this component multiple times on a page. e.g. var TodoItem = React.createClass({ ... render:function(){ function oneSecLater(){ setTimeout(function(){ //select the current className? this doesn't work, but it gives you an idea what I want. document.getElementsByClassName('name').style.backgroundColor = "red"; )}, 1000); } return( <div className='name'>{this.oneSecLater}</div> ) }) Here, no need to use setTimeout. There are lifecycle methods for

JSX 到 JS 的转换

牧云@^-^@ 提交于 2019-12-03 21:01:01
在写react代码的时候,大部分同学应该都是写JSX。因为相比于写纯JavaScript。写JSX为我们去写组件,比写一些在JavaScript当中写类似于html结构的这种代码是要方便非常非常多的,他的可阅读性,可维护性都要高很多的。那么JSX他的魔力在哪里,能够让我们在JS里面写html代码。 JSX相对于JavaScript来讲,他的唯一的一个区别就是他可以写类似于html的标签。 https://www.babeljs.cn/repl 在这个网址写示例 左边 <div></div> 右边 "use strict"; React.createElement("div", null); 在左边我们声明了html的标签,他在右边会返回一个我们在react当中我们需要返回的一个对象。这就是 JSX 到 JS 的一个转换过程 我们会发现通过这种标签写的,他最终都会转换成 React.createElement 。里面的标签名,属性,内容都会通过参数给他。 左边 <div id='div' key='key'>test</div> 右边 React.createElement("div", { id: "div", key: "key" }, "test"); 会把所有的属性放在第二个参数,第二个参数是个对象,都会加在这个对象里面给他。第三个参数在 React 中称为 children

react js - use svg linear gradient not working

落花浮王杯 提交于 2019-12-03 16:39:12
i have a circle and a grradient to fill in it, i put in the gradient and call him in path by style fill. import React,{PropTyoes} from 'react'; import {connect} from 'react-redux'; import * as Actions from '../controllers/Actions'; export default class MyComp extends React.Component { constructor(props, context){ super(props, context); } render(){ return ( <svg xmlns="http://www.w3.org/2000/svg" xmlnsXlink="http://www.w3.org/1999/xlink" viewBox="0 0 983.4 983.4"> <g> <linearGradient id="linear-gradient" gradientUnits="userSpaceOnUse" x1="1041.6901" y1="169.485" x2="1383.9301" y2="169.485"

ReactJS map through Object

送分小仙女□ 提交于 2019-12-03 15:33:27
问题 I have a response like this: I want to display the name of each object inside this HTML: {subjects.map((item, i) => ( <li className="travelcompany-input" key={i}> <span className="input-label">{ item.name }</span> </li> ))} But it throws an error of subjects.map is not a function . First, I have to define the keys of the objects where it creates an array of keys, where I want to loop through and show the subject.names . What I also tried is this: {Object.keys(subjects).map((item, i) => ( <li