jsx

ReactJS JSX Syntax for If-Else

こ雲淡風輕ζ 提交于 2019-12-24 07:59:05
问题 I have the following jsx code, I am trying to put some code out of the mapping function, but getting errors with the jsx syntax, I want this block displayed only if there is some data.. {this.props.someData ? <div className="container"> <h3>Heading</h3> <div className="block1"> <button>one</button> <buttontwo</button> </div> this.props.someData.map((response, index) => { return ( <div className="Block2"> <div key={index}> <span>{response.number}</span> </div> </div> </div> ); }) : ''} 回答1:

React中的虚拟DOM

扶醉桌前 提交于 2019-12-24 07:50:11
当组件当state和props发生变化当时候,组件当render函数就会重新执行,组件就会被重新渲染,react中实现这种重新渲染,他的性能是非常高的,因为他引入了一个虚拟Dom的概念,那么什么是虚拟的Dom,为什么虚拟Dom带来了变革性当改变 当state发生变化,render函数会重新执行,重新的去渲染一次页面。假设没有react,我们自己要实现这个功能,那应该怎么去实现呢?我们来理个思路 1、state 数据 2、jsx模板 3、把数据 + 模板相结合,生成真实的dom,来显示 4、state 发生改变 5、数据 + 模板 结合,生成真实的dom,替换原始的dom 这么做,有一个致命的缺陷,是什么呢? 第一次生成了一个完整的dom片段 第二次生成了一个完整的dom片段 第二次的dom替换第一次的dom,非常耗性能。当我们inputvalue发生变化的时候,其实页面上只有input这块dom发生变化,如果是这种做法,虽然是input的数据发生变化,但是我需要整个dom区块重新生成一次,替换掉原始的dom情况,这样的话,生成一个完整的dom片段很耗性能,替换掉一个完整的dom片段,也很耗性能。那我们改良一下 1、state 数据 2、jsx模板 3、把数据 + 模板相结合,生成真实的dom,来显示 4、state 发生改变 5、数据 + 模板 结合,生成真实的dom

How to add a ClassName and remove it onScroll event in React.JS?

眉间皱痕 提交于 2019-12-24 05:02:12
问题 I'm trying to make a Sticky Header that can change its background-color based on his position on the page. To do that, I'm trying to add a className "active" to my Styled Component "StyledHeader" that will appears when the scrollPositionY is above 400px and disappear when below. In other words, what I want to do is something like this but using React.JS, JSX syntax and Styled Components. Here's what I have for now: import { Link } from '@reach/router'; import DuskLogo from '../images/dusk

How to add a ClassName and remove it onScroll event in React.JS?

混江龙づ霸主 提交于 2019-12-24 05:01:12
问题 I'm trying to make a Sticky Header that can change its background-color based on his position on the page. To do that, I'm trying to add a className "active" to my Styled Component "StyledHeader" that will appears when the scrollPositionY is above 400px and disappear when below. In other words, what I want to do is something like this but using React.JS, JSX syntax and Styled Components. Here's what I have for now: import { Link } from '@reach/router'; import DuskLogo from '../images/dusk

Is there a compiler from js to jsx for React?

扶醉桌前 提交于 2019-12-24 03:19:11
问题 Does someone know a compiler to convert React/JS to React/JSX. At work I am given a task to maintain a react app that is writen in the old fashioned javascript style. Recently I began leaning and using EcmaScript6 but at work this old JS is very messy. What can I do to easily understand what's going on with this project and code base written in this ugly style? Any help is highly appreciated! 回答1: Some googling led me to this https://github.com/JoeStanton/babel-transform-js-to-jsx. Haven't

Return JSX from function

六月ゝ 毕业季﹏ 提交于 2019-12-24 00:01:23
问题 I have a a component that needs to check for multiple options and return based on those options. Therefore I have created an outside function that is called in my component return statement to determine the format that will be returned: render() { const { policy } = this.props; let deployment = policy.Deployment; let value = policy.value; let policyLegend = deployment.policyLegend; let policyObj = this.valueToPolicy(policyLegend, value); console.log(policyObj); if(policy.name == "SP4") {

react Props 验证 propTypes,

点点圈 提交于 2019-12-23 20:28:11
<body> <!-- React 真实 DOM 将会插入到这里 --> <div id="example"></div> <!-- 引入 React --> <script src="src/libs/react.js"></script> <!-- 引入 JSX 语法格式转换器 --> <script src="src/libs/JSXTransformer.js"></script> <script src="src/libs/jquery.js"></script> <!-- 注意:script 需要注明 type 为 text/jsx 以指定这是一个 JSX 语法格式 --> <script type="text/jsx"> var title = "my react"; var MyTitle = React.createClass({ propTypes : { title:React.PropTypes.string.isRequired, }, render : function () { return ( <h1>1111 : {this.props.title}</h1> ) } }) React.render( <MyTitle title={title} />, document.getElementById('example') ) </script>

2.ReactJS基础(虚拟DOM,JSX语法)

孤人 提交于 2019-12-23 17:53:19
将脚手架(create-react-app)创建的todolist项目精简为hello world示例 即,删除自动生成的样式文件、logo.svt、App.test.js、serviceWorker.js等文件,并精简App.js和index.js里的代码,结果如下: npm run start 后,浏览器界面仅显示纯文本 Hello world ! 一,虚拟DOM 首先,看一段html片段 <div id="container"> <p>这里是p标签里的文本节点</p> <ul> <li class="item">这里是li标签里的文本节点</li> <li class="item">这里是li标签里的文本节点</li> <li class="item">这里是li标签里的文本节点</li> </ul> </div> 上面的html片段会最终生成为DOM对象,并由浏览器渲染到页面上。 而虚拟DOM,就是用一个JS对象模拟上面的DOM元素和结构: ['div', { id: 'container' }, [ ['p', {}, '这里是p标签里的文本节点'], ['ul', {},[ ['li', { class: 'item' }, '这里是li标签里的文本节点'], ['li', { class: 'item' }, '这里是li标签里的文本节点'], ['li', {

TypeScript 3: JSX element type 'Component' does not have any construct or call signatures. [2604]

旧城冷巷雨未停 提交于 2019-12-23 10:18:03
问题 I'm trying to pass a variable of type React.Component (or React.FunctionComponent) into a Route, like this: import React from 'react'; import { Route } from 'react-router-dom'; type PrivateRouteProps = { component: React.Component | React.FunctionComponent; isAuthenticated: boolean; login: (...args: any[]) => any; path: string; }; const PrivateRoute: React.FunctionComponent<PrivateRouteProps> = ({ component: Component, isAuthenticated, login, path, ...rest }) => { return ( <Route path={path}

How can I deal with a long className in React JSX?

谁说胖子不能爱 提交于 2019-12-23 09:38:05
问题 Let's say I'm rendering this component in React JSX: render() { return ( <h1 className="col-xs-6 col-xs-offset-3 col-md-4 col-md-offset-4 col-lg-2 col-lg-offset-5">Some text</h1> ); } The classes trigger my JS linter as a line that's too long, and it's very hard to read. How can I separate a long className property in a React component into multiple lines without breaking JSX syntax or triggering a different error in a JS linter? (I'm using ESLint). 回答1: Another Cleaner method will be to