react遇到问题

241 阅读1分钟

1.阻止事件冒泡

<ul><li onClick={this.selected}><span>abcde</span><a>delete</></li></ul>
selected=(e)=>{
     e.preventDefault();
        //TODO:继续逻辑
}

2.React的onClick函数如何传参

通过bind(this,参数)的方法就可以在onClick中传参了
 
<Button onClick={this.showEdit.bind(this, 'add')}>添加用户</Button>
 
 
showEdit = (type) => {
	console.log(type)  // 这里面的type就是'add'
	this.setState({ modalType: type, modalVisible: true })

}

3.动态添加class

<div className={this.state.isStyle?styles.tabsByStyle:null}> 
 
2.添加style 样式
this.state={
    tabsByStyle:{
                    "position": "fixed",
                    "top": "0px",
                    "zIndex": "9999",
                    "marginBottom": "0.8rem"
    }
}
 
<div style={this.state.tabsByStyle}>