amandaxcy / javascript Goto Github PK
View Code? Open in Web Editor NEWreact
react
https://github.com/yahoo/react-intl
React-intl是FormatJS的一部分,内置实现Date/Number/Time的国际格式化;
可以自定义映射关系,完成值对之间的替换(这是这篇文章的主要内容);
通过获取浏览器的language来设置显示中文/英文(通过自定义映射,而非自动全文转换);
常用于实现静态内容,如按钮文字,公司名称的转换;
/*在rect-intl语言中,只支持简单的json数据,通常为了可读性我们会写成如下格式,可使用falt转化*/
//flat:https://github.com/hughsk/flat
import flatten from 'flat';
const zh = {
http: {
server_error: '与服务器的连接出现问题,请稍后再试。',
rate_limited: '访问次数过多,请稍后再试。'
},
button: {
next: '下一步',
finish: '完成'
},
}
export default flatten(zh);
输出如下:
{
'http.server_error':'与服务器的连接出现问题,请稍后再试。',
'http.rate_limited':'访问次数过多,请稍后再试。',
'button.next':'下一步',
'button.finish':'完成'
}
import React from 'react';
import ReactDOM from 'react-dom';
// 从'react-intl'中引入addLocaleData,IntlProvider,FormattedMessage三个格式化组件;
import {addLocaleData,IntlProvider,FormattedMessage} from 'react-intl';
/*
*引入与navigator.languages[0]所对应的语言;
*如果没有引入对应的语言,会使用默认的“en”;
*导致FormattedMessage的映射不会成功;
*/
import zh from 'react-intl/locale-data/zh';
import en from 'react-intl/locale-data/en';
/*
*引入自定义的映射表;
*通过与FormattedMessage的id值来当索引映射返回值;
*/
import zh_CN from './locale/zh_CN';
import en_US from './locale/en_US';
import App from './components/app';
/*
*messages是render()时IntlProvider组件所传的props,属性名固定:messages;
*messages返回值为映射表,比如:'react-intl/locale-data/zh'&&'./locale/zh_CN';
*/
let messages = {};
messages["en-US"] = en_US;
messages["zh-CN"] = zh_CN;
/*
*获取浏览器设置的语言;
*按我demo中的设置,返回["zh-CN", "zh", "en-US", "en"],排序与语言设置顺序一直;
*/
const languages = navigator.languages;
const currentLang = languages[0];
// 载入语言数据;
//载入一个addLocaleData(zh);
//载入多个;
addLocaleData([...zh,...en]);
ReactDOM.render(
// IntlProvider为'react-intl'指定的包裹组件名;
<IntlProvider locale={currentLang} messages={messages[currentLang]}>
<App />
</IntlProvider>,
document.body
)
/*基本用法:*/
import React, {Component} from 'react';
import {FormattedMessage} from 'react-intl';
class App extends Component {
constructor(props) {
super(props);
}
render() {
/*
*FormattedMessage组件中的信息parser后显示以<span>包裹的文本;
*可以通过tagName指定其它标签包裹;
*以id属性的值"hello"为索引——索引到自定义的映射表'./locale/zh_CN'中去;
*messages['hello']——messages为父组件IntlProvider的props的messages属性;
*若没有上述的返回值,则显示defaultMessage的值"React Intl Translations Example";
*/
/*
*FormattedMessage添加子元素或ReactElement;
* <FormattedMessage id="hello">
* {(formattedValue)=>(
* <em>{formattedValue}</em>
* )}
* </FormattedMessage>
*/
return (
<div>
<h1>
<div>
<FormattedMessage
id="hello"
defaultMessage="React Intl Translations Example"
/>
</div>
</h1>
<h4>
<FormattedMessage
tagName = 'p'
id='superHello'
defaultMessage="Locales:北京"
values={{
someone:'zxdobest'
}}
/>
</h4>
<h2>
<FormattedMessage id="hello">
{(formattedValue)=>(
<em>{formattedValue}</em>
)}
</FormattedMessage>
</h2>
</div>
);
}
}
export default App;
import React from 'react';
import { injectIntl, intlShape, FormattedMessage } from 'react-intl';
const ChildComponent = ({ intl }) => {
//传入的{intl}名称不可更改;
const getMessage = intl.messages;
return(
<section>
{/*
*通过intl.messages获取映射属性的方法;
* 可以和任意内容(如:HTML标签)组合;
* 如果你的key里有包含.,只能使用[]取值
* */}
<a>{getMessage['hello']}</a>
{/*通过FormattedMessage格式化,可以传入参数{values}*/}
<FormattedMessage
tagName = 'p'
id='superHello'
defaultMessage="Locales:北京"
values={{
someone:'zxdobest'
}}
/>
{/*
*这种结构,类同于intl.messages.superHello可以和其它内容任意组合;
*能且仅能获取superHello的映射;
* 目前尚未找到如果在第一种方法中传入values;
*/}
<FormattedMessage id="superHello" values={{
someone:'mihuartuanr'
}}>
{(formattedValue)=>(
<p>{formattedValue}</p>
)}
</FormattedMessage>
</section>
);
}
ChildComponent.propTypes = {
intl: intlShape.isRequired
}
export default injectIntl(ChildComponent);
1.var没有块级作用域,let有
for(var i=0;i<10;i++){
var a = 'a';
}
console.log(a);
这里已经跳出循环了,确能访问到a,把var 换成let 得到的结果是undefined
for (var i = 0; i < 3; i++) {
setTimeout(function () {
console.log(i)
}, 1000);
}
这里你希望是输出1,2,3,但却得3个3 把var 换成let 就会得到你期望的
2.重复定义会抛出错
3.全局变量不再属于window对象:在全局let声明的变量不再属于顶层对象(window)
4.let不再有变量提升
console.log(c1);//这里会报错 ReferenceError: c1 is not defined
let c1=1
console.log(c1);//undefined
var c1=1
var是先申明再赋值,
var c1=1
其实是相当于:
var c1
console.log(c1)
c1=1
componentDidCatch不能捕获所有错误,组件上绑定的事件的回调方法里的错误 捕获不到
视频:(https://pan.baidu.com/s/1jImXhLO)[https://pan.baidu.com/s/1jImXhLO]
在 render 方法里可以返回多个节点数组
render(){
return [
<li>Apple<li>
<li>Banna<li>
]
}
(https://pan.baidu.com/s/1cg1OZ0)[https://pan.baidu.com/s/1cg1OZ0]
https://reactjs.org/docs/composition-vs-inheritance.html
react,推荐组合,不推荐集成
父组组件卸载
type 发生变化(组件变化)
key 发生变化
在React16中,不在推荐使用this.ref.node,因为在后面的版本中可能要被移出,现在推荐使用回调函数
<li ref={(li)=>this.liNode=li}
[1,2,3]
new Array(1,2,3)
new Object([1,2,3])
1.在组件对props的修改
VUE:修改props会报错
react:不会
2.在react中,你想改变state时,不会重复更新这个组件时,你需要在componetWillReceiveProps中返回fase,但在vue中只需要在标签或组件上增加一个v-once就可以搞定
bug记录:
【日期】:
【问题】:
【原因】:
【怎么发现的】:
【修复】
【在哪些文件修改了】
【我导致的】
【解决bug时间】
【教训】
version中 ~ 和 ^ 的作用和区别是什么呢?
~ 会匹配最近的小版本依赖包,比如~1.2.3会匹配所有1.2.x版本,但是不包括1.3.0
^ 会匹配最新的大版本依赖包,比如^1.2.3会匹配所有1.x.x的包,包括1.3.0,但是不包括2.0.0
(只渲染元素和组件一次。随后的重新渲染,元素/组件及其所有的子节点将被视为静态内容并跳过。这可以用于优化更新性能。)
当你在标签上设置了v-once之后,再去操作数据时,还会触发beforeUpdate、updated,但Virtual Dom比较中,它不会进行任务比对,只是把oldNode复制到了Vnode上,相当于在react中,componentWillReceivePorps里返回true,但vue中更方便些
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.