jsx

Vue之render渲染函数和JSX的应用

亡梦爱人 提交于 2019-11-29 09:51:51
一.模板缺陷 模板的最大特点是扩展难度大,不易扩展。可能会造成逻辑冗余 <Level :type="1">哈哈</Level> <Level :type="2">哈哈</Level> <Level :type="3">哈哈</Level> Level组件需要对不同的type产生不同的标签 <template> <h1 v-if="type==1"> <slot></slot> </h1> <h2 v-else-if="type==2"> <slot></slot> </h2> <h3 v-else-if="type==3"> <slot></slot> </h3> </template> <script> export default { props: { type: { type: Number } } }; </script> 二.函数式组件 函数式组件没有模板,只允许提供render函数 export default { render(h) { return h("h" + this.type, {}, this.$slots.default); }, props: { type: { type: Number } } }; 复杂的逻辑变得非常简单 三.JSX应用 使用jsx会让代码看起来更加简洁易于读取 export default { render(h) { const

图片上传转base64

我的梦境 提交于 2019-11-29 09:40:37
做的过程中本来想用taro-ui里的那个图片上传,但是样式想自定义没搞定,结果后来就用Taro.chooseImage了。h5模式返回的是一个blob对象,然后自己转成base64了。微信小程序自己有方法。原来想用multipart的方式,奈何后端要求一次性要把所有的参数上送,直接上送是blob:http//xxxxx最后只好约定前端转成base64上送了。 jsx: <View className='flex-center real-card-container mt60' onClick={ this.uploadCard.bind(this,'fileJust') }> <Image className='real-card-img' src={require('../../assets/images/cardR.png')}></Image> <Text className='real-card-title'>请拍摄有人像的一面</Text> </View> js: uploadCard(fileType){ let _this = this; Taro.chooseImage({ count: 1,// 默认9 sizeType: ['original', 'compressed'],// 可以指定是原图还是压缩图,默认二者都有 sourceType: ['album',

Set component's props dynamically

删除回忆录丶 提交于 2019-11-29 06:45:37
问题 I need to set component's props after it is stored in a variable, here is pseudo code: render(){ let items = [{title:'hello'}, {title:'world'}]; let component = false; switch (id) { case 1: component = <A /> break; case 2: component = <B /> break; } return( items.map((item, index)=>{ return( <span> {/* SOMETHING LIKE THIS WOULD BE COOL - IS THAT EVEN POSSIBLE*/} {component.props.set('title', item.title)} </span>11 ) }) ) } Inside return I run a loop where I need to set props for the component

How do I add attributes to existing HTML elements in TypeScript/JSX?

一笑奈何 提交于 2019-11-29 03:27:52
Anyone know how to properly add/extend all native HTML element attributes with custom ones? With the TypeScript documentation for merging interfaces , I thought that I could just do this: interface HTMLElement { block?: BEM.Block; element?: BEM.Element; modifiers?: BEM.Modifiers; } <div block="foo" />; // error But I get the following Intellisense error in vscode 1.6.1 (latest): [ts] Property 'block' does not exist on type 'HTMLProps'. The HTMLProps to which they are referring is React.HTMLProps<T> and the div element is declared to use it like so: namespace JSX { interface IntrinsicElements {

How to make a sticky footer in react?

回眸只為那壹抹淺笑 提交于 2019-11-29 03:21:40
问题 I've made a sticky footer higher-level component that wraps other components inside itself: Footer.js //this is a higher-order component that wraps other components placing them in footer var style = { backgroundColor: "#F8F8F8", borderTop: "1px solid #E7E7E7", textAlign: "center", padding: "20px", position: "fixed", left: "0", bottom: "0", height: "60px", width: "100%", }; const Footer = React.createClass({ render: function() { return ( <div style={style}> {this.props.children} </div> ); } }

一看就懂ReactJS

冷暖自知 提交于 2019-11-29 02:36:21
现在最热门的前端框架有AngularJS、React、Bootstrap等。自从接触了ReactJS,ReactJs的虚拟DOM(Virtual DOM)和组件化的开发深深的吸引了我,下面来跟我一起领略 ReactJS的风采吧~~ 章有点长,耐心读完,你会有很大收获哦~ 一、ReactJS简介 React 起源于 Facebook 的内部项目,因为该公司对市场上所有 JavaScript MVC 框架,都不满意,就决定自己写一套,用来架设 Instagram 的网站。做出来以后,发现这套东西很好用,就在2013年5月开源了。由于 React 的设计思想极其独特,属于革命性创新,性能出众,代码逻辑却非常简单。所以,越来越多的人开始关注和使用,认为它可能是将来 Web 开发的主流工具。 ReactJS官网地址: http://facebook.github.io/react/ Github地址: https://github.com/facebook/react 二、对ReactJS的认识及ReactJS的优点 首先,对于React,有一些认识误区,这里先总结一下: React不是一个完整的MVC框架,最多可以认为是MVC中的V(View),甚至React并不非常认可MVC开发模式; React的服务器端Render能力只能算是一个锦上添花的功能,并不是其核心出发点

React-Native Button style not work

我只是一个虾纸丫 提交于 2019-11-29 02:10:52
问题 Import_this import {AppRegistry, Text, View, Button, StyleSheet} from 'react-native'; This my React Button code But style not working Hare ... <Button onPress={this.onPress.bind(this)} title={"Go Back"} style={{color: 'red', marginTop: 10, padding: 10}} /> Also I was try by this code <Button containerStyle={{padding:10, height:45, overflow:'hidden', borderRadius:4, backgroundColor: 'white'}} style={{fontSize: 20, color: 'green'}} onPress={this.onPress.bind(this)} title={"Go Back"} > Press me!

Iterating over JSON in React

末鹿安然 提交于 2019-11-28 23:56:16
I have following code: export class Highlights extends React.Component { render() { return ( <div> {JSON.stringify(this.props.highlights_data.data)} </div> ) } } This prints out the following: {"active":{"label":"Active","value":"12"},"automatic":{"label":"Automatic","value":"8"},"waiting":{"label":"Waiting","value":"1"},"manual":{"label":"Manual","value":"3"}} How can I iterate over the highlights_data.data props to call another Component passing down label and value ? Except for @Dan's answer, I don't believe the other answers are any helpful/useful to you as they don't iterate through your

React 学习记录(一)

旧时模样 提交于 2019-11-28 21:24:02
入门 (环境配置,Hello World) 一、安装 nodeJS   下载目录: http://nodejs.cn/download/ 二、配置环境    1.新建项目目录:E:\study\React\demos\Hello    2.CMD至项目目录,使用 npm init 命令生成package.json文件,生成的文件代码如下: {    "_note": "名称中不能有大写字母,其余的可以自动生成",    "name": "helloworld",    "version": "1.0.0",    "description": "a new demo",    "main": "index.js",    "private": true,    "scripts": {      "test": "echo \"Error: no test specified\" && exit 1"    },    "keywords": [      "react"    ],    "author": "cheny",    "license": "ISC" }    3.安装开发过程中所需要的包      3.1安装react开发包 npm i react --save ,这里使用的是 --save 意思是安装的这个包在发布后也依然被依赖。      3