1.JSX 防注入攻击

你可以放心地在 JSX 当中使用用户输入

 const title = response.potentiallyMaliciousInput;
// 直接使用是安全的:
const element = <h1>{title}</h1>;

React DOM 在渲染之前默认会过滤所有传入的值。它可以确保你的应用不会被注入攻击。所有的内容在渲染之前都被转换成了字符串。这样可以有效地防止 XSS(跨站脚本)攻击。

2.事件处理

在 React 中另一个不同是你不能使用返回 false 的方式阻止默认行为。你必须明确的使用 preventDefault

function ActionLink() {
function handleClick(e) {
e.preventDefault();
console.log('The link was clicked.');
} return (
<a href="#" onClick={handleClick}>
Click me
</a>
);
}

在这里,e 是一个合成事件。React 根据 W3C spec 来定义这些合成事件,所以你不需要担心跨浏览器的兼容性问题。

3.事件处理的this

 class Toggle extends React.Component {
constructor(props) {
super(props);
this.state = {isToggleOn: true}; // This binding is necessary to make `this` work in the callback
this.handleClick = this.handleClick.bind(this);
} handleClick() {
this.setState(prevState => ({
isToggleOn: !prevState.isToggleOn
}));
} render() {
return (
<button onClick={this.handleClick}>
{this.state.isToggleOn ? 'ON' : 'OFF'}
</button>
);
}
} ReactDOM.render(
<Toggle />,
document.getElementById('root')
);

你必须谨慎对待 JSX 回调函数中的 this类的方法默认是不会绑定 this 的。如果你忘记绑定 this.handleClick 并把它传入 onClick, 当你调用这个函数的时候 this 的值会是 undefined

通常情况下,如果你没有在方法后面添加 () ,例如 onClick={this.handleClick},你应该为这个方法绑定 this

解决方法:

  1.bind 绑定

   constructor(props) {
super(props);
this.state = {isToggleOn: true}; // This binding is necessary to make `this` work in the callback
this.handleClick = this.handleClick.bind(this);
}

  2.如果你正在使用实验性的属性初始化器语法,你可以使用属性初始化器来正确的绑定回调函数:----------这个语法在 Create React App 中默认开启。

   handleClick = () => {
console.log('this is:', this);
}

  3.箭头函数

   handleClick() {
console.log('this is:', this);
} render() {
// This syntax ensures `this` is bound within handleClick
return (
<button onClick={(e) => this.handleClick(e)}>
Click me
</button>
);
}

4.向事件处理程序传递参数

通常我们会为事件处理程序传递额外的参数。例如,若是 id 是你要删除那一行的 id,以下两种方式都可以向事件处理程序传递参数:

  1.箭头函数

参数 e 作为 React 事件对象将会被作为第二个参数进行传递。

<button onClick={(e) => this.deleteRow(id, e)}>Delete Row</button>

  2.bind绑定

通过箭头函数的方式,事件对象必须显式的进行传递,但是通过 bind 的方式,事件对象以及更多的参数将会被隐式的进行传递。

<button onClick={this.deleteRow.bind(this, id)}>Delete Row</button>

值得注意的是,通过 bind 方式向监听函数传参,在类组件中定义的监听函数,事件对象 e 要排在所传递参数的后面

preventPop(name, e){    //事件对象e要放在最后
e.preventDefault();
alert(name);
} <a href="https://reactjs.org" onClick={this.preventPop.bind(this,this.state.name)}>Click</a>

5.多个输入的解决方法

当你有处理多个受控的input元素时,你可以通过给每个元素添加一个name属性,来让处理函数根据 event.target.name的值来选择做什么。

class Reservation extends React.Component {
constructor(props) {
super(props);
this.state = {
isGoing: true,
numberOfGuests: 2
}; this.handleInputChange = this.handleInputChange.bind(this);
} handleInputChange(event) {
const target = event.target;
const value = target.type === 'checkbox' ? target.checked : target.value;
const name = target.name; this.setState({
[name]: value
});
} render() {
return (
<form>
<label>
Is going:
<input name="isGoing" type="checkbox" checked={this.state.isGoing} onChange={this.handleInputChange} />
</label>
<br />
<label>
Number of guests:
<input name="numberOfGuests" type="number" value={this.state.numberOfGuests} onChange={this.handleInputChange} />
</label>
</form>
);
}
}

最新文章

  1. Wrangle – 响应式的,触摸友好的多选插件
  2. [No00003E]26个字母暗藏的单词秘密
  3. 搜索 录音功能 Android api
  4. [linux]树莓派入手体验和系统安装
  5. 一个代价11万的bug
  6. java的包装类(转)
  7. [POJ1830]开关问题(高斯消元,异或方程组)
  8. string(Integer)类的equals和==区别和联系(验证密码的时候用得到)
  9. 汇编debug与masm命令
  10. 记一次easywechat企业付款问题
  11. 2018牛客网暑假ACM多校训练赛(第十场)F Rikka with Line Graph 最短路 Floyd
  12. Bootstrap学习记录-3.Badge、Breadcrumb、Buttons、 Button Group、Card、Carousel
  13. vue的生命周期(lifecycle)
  14. requests(第三方模块) 请求、登录、下载网页
  15. iOS蓝牙空中升级(固件升级)
  16. 转发 DDoS攻防战 (一) : 概述
  17. 洛咕 P4199 万径人踪灭
  18. 【CodeForces】741 D. Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths(dsu on tree)
  19. Office WORD如何在图片上添加文字
  20. P2750 贰五语言Two Five USACO5.5 记忆化搜索

热门文章

  1. map member functions
  2. python-2:爬取某个网页(虎扑)帖子的标题做词云图
  3. python格式化当前时间,暂停一秒输出
  4. kafka 教程(四)-原理进阶
  5. 修改jar包package目录结构操作方法
  6. git 查看对比的方法log diff
  7. N4复习考试总结
  8. Delphi上机步骤
  9. python自动化测试—配置文件的使用
  10. Oracle 之 触发器