jsx

Display images in React using JSX without import

匿名 (未验证) 提交于 2019-12-03 02:24:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: The problem i face is with img tag. When a single is concerned, The below code loads the image: import image1 from './images/image1.jpg; <img src={image1} /> But the below code doesn't load: <img src={'./images/image1.jpg'}/> or <img src='./images/image1.jpg'/> I need to loop through json , something like [{'img':'./images/image1.jpg','name':'AAA'},{'img':'./images/image2.jpg','name':'BBB'}] and display each of them as image with name as footer. Looping is fine but images doesn't load.It is not actually possible for myself to import every

Converting React function component to class component issue

大城市里の小女人 提交于 2019-12-03 02:15:28
I have the following react functional component to help support authentication required routes with react-router. const PrivateRoute = ({ component: Component, ...rest }) => ( <Route {...rest} render={props => ( isAuthenticated() ? ( <Component {...props}/> ) : ( <Redirect to={{ pathname: '/login', state: {from: props.location } }}/> ) )}/> ) I need to convert this from a functional component to a class component so I can take advantage of the componentDidMount method of React.Component. Unfortunately I'm having trouble figuring out how to migrate this. If I take it as is I need to replicate

React基础学习

前提是你 提交于 2019-12-03 02:05:18
1. 什么是React? React 是一个用于构建用户界面的JavaScript库 核心专注于视图,目的实现组件化开发 2. 组件化的概念 我们可以很直观的将一个复杂的页面分割成若干个独立组件,每个组件包含自己的逻辑和样式 再将这些独立组件组合完成一个复杂的页面。 这样既减少了逻辑复杂度,又实现了代码的重用 可组合:一个组件可以和其他的组件一起使用或者可以直接嵌套在另一个组件内部 可重用:每个组件都是具有独立功能的,它可以被使用在多个场景中 可维护:每个小的组件仅仅包含自身的逻辑,更容易被理解和维护 3.搭建React开发环境 cnpm i create-react-app -g create-react-app react2019 cd react2019 yarn start 默认会自动安装React,react由两部分组成,分别是: react.js 是 React 的核心库 react-dom.js 是提供与DOM相关的功能,内部比较重要的方法是render,它用来向浏览器里插入DOM元素 4.JSX 4.1 什么是JSX 是一种JS和HTML混合的语法,将组件的结构、数据甚至样式都聚合在一起定义组件 JSX其实只是一种语法糖,最终会通过 babeljs 转译成 createElement 语法,以下代码等价 ReactDOM.render( <h1>Hello</h1>

React JSX: Access Props in Quotes

匿名 (未验证) 提交于 2019-12-03 01:58:03
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: In JSX, how do you reference a value from props from inside a quoted attribute value? For example: The resulting HTML output is: 回答1: React (or JSX) doesn't support variable interpolation inside an attribute value, but you can put any JS expression inside curly braces as the entire attribute value, so this works: 回答2: If you want to use the es6 template literals, you need braces around the tick marks as well: 回答3: If you're using JSX with Harmony, you could do this: Here you are writing the value of src as an expression. 回答4: Best practices

ReactJS: “Uncaught SyntaxError: Unexpected token &lt;”

匿名 (未验证) 提交于 2019-12-03 01:52:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I am trying to get started building a site in ReactJS. However, when I tried to put my JS in a separate file, I started getting this error: "Uncaught SyntaxError: Unexpected token I tried adding /** @jsx React.DOM */ to the top of the JS file, but it didn't fix anything. Below are the HTML and JS files. Any ideas as to what is going wrong? HTML Page JS /** * @jsx React.DOM */ var Lander = React.createClass({ render: function () { var info = "Lorem ipsum dolor sit amet... "; return( {info} ); } }); EDIT: I realized that I need to add type=

How to convert react JSX files to simple JavaScript file [offline transformation]

匿名 (未验证) 提交于 2019-12-03 01:37:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I am new to react.js. Just started learning it. So my question may seems to be stupid but please answer it. I have created a simple .js file with following code: var Hello=React.createClass({ render: function(){ return (<h1>Hello World</h1>); } }); It is in JSX syntax and to convert it to simple JavaScript file I used following command: babel --preset react JSX_Files --watch --out-dir public/javascripts Here JSX_Files is my source folder and public/javascripts is the folder where I want my transformed .js files. But I'm getting following

What does the error “JSX element type '…' does not have any construct or call signatures” mean?

匿名 (未验证) 提交于 2019-12-03 01:23:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I wrote some code: function renderGreeting ( Elem : React . Component ) { return Hello , ! ; } I'm getting an error: JSX element type Elem does not have any construct or call signatures What does it mean? 回答1: This is a confusion between constructors and instances . Remember that when you write a component in React: class Greeter extends React . Component { render () { return Hello , { this . props . whoToGreet } ; } } You use it this way: return ; You don't use it this way: let Greet = new Greeter (); return ; In the first example

Need help on React Js

自作多情 提交于 2019-12-03 01:02:35
问题 I am new to React Js and my code is not working. Please have a look below: This is my script file Main.jsx . This file is compiled by react and output is put in main.js file under 'dist' folder. var react = require("react"), reactDom = require("react-dom"); var myComponent = react.createClass({ render: function () { return <div><h4>I am rendered by react.</h4></div>; } }); reactDom.render(<myComponent />, document.getElementById("basicDiv")); It is Index.html <html> <head> <base href="./" />

JSX

匿名 (未验证) 提交于 2019-12-03 00:40:02
把不是ES5的代码转换成ES5的代码,是一个JavaScript编译器。 代码在浏览器中运行之后会生成一个js对象,含有两个属性: type 和 children 。然后读取 type 属性值,通过DOM操作添加元素。 这个js对象就是传说中的 virtual DOM 。 因为babel将JSX代码转换成js代码后,需要用到React。 将表达式放到花括号中; 并列标签必须要被一个爸爸(div)包住,不想要爸爸的话,可以找一个假的爸爸:import导入Fragment,使用 <Fragment><Fragment/> 包住并列的孩子们; 一个花括号代表着表达式; 两个花括号才表示对象; 行内样式要这样写: style = {{}} ; 行内样式中,比如 font-size 要写成 fontSize (即驼峰式); 行内属性中 class 属性不能用,要用 className 。但是id还是可以照常使用的; JSX 原文:https://www.cnblogs.com/endymion/p/9285885.html

React学习记录

匿名 (未验证) 提交于 2019-12-03 00:37:01
一、 Props: props是实现组件组合的关键。props是React的一种从父组件向子组件传输数据的机制。他们在子组件中是不能被修改的;props由父组件传输出去,也被父组件缩“拥有”。 二、 state: React的组件可以在this.state里保存可变的数据 三、 react中的事件:React中的事件,是通过自动事件委托来实现高性能。React不会将事件处理程序附加到节点自身。相反,他会讲一个单独的事件处理侦听器附加到文档的根节点,当事件触发的时候,React将他映射到适当的组件元素。当组件被卸载的时候,React还会自动的移除对应的事件侦听器。(事件的命名:原生的HTML不是驼峰命名,React中的事件的名称是驼峰命名)。 四、 JSX编写代码时注意规范: 1.标签特性才去驼峰式大小写的风格 2.所有元素都必须闭合(没有闭合标签就自闭合) 3.特性名称基于DOM API 五、 JSX的使用小技巧: 1.单一根节点: React的render中的return方法只能渲染一个跟节点,即只能有一个父级大标签。 2.条件语句:在编写jsx的时候if可能会被转化为非法的js语句,因此可以使用三元表达式来解决条件判断中出出现的问题,还可以将条件外置,在jsx外部判断之后赋值,然后在jsx中渲染。 六、 空格:JSX不会再多行之间自动渲染一个空格,因此想要一个空格,可以手动插入