Reactjs笔记

4 minute read

umiJs + antd_mobile + dvaJs

多类名及动态加载样式方法

覆盖组件样式

方法很简单,有两点需要注意:

  1. 引入的 antd 组件类名没有被 CSS Modules 转化,所以被覆盖的类名 .ant-select-selection 必须放到 :global 中。

  2. 因为上一条的关系,覆盖是全局性的。为了防止对其他 Select 组件造成影响,所以需要包裹额外的 className 限制样式的生效范围。 e.g.

1.customSelect {
2  :global {
3    .ant-select-selection {
4      max-height: 51px;
5      overflow: auto;
6    }
7  }
8}

model:模型

   模型通常认为是视图(view)的内核,模型就是指视图的数据

// ES7新标准:装饰器(Decorator) @connect(state => ({ user: state.user, }))

// 如果您不喜欢ES7装饰器,那好办,我就帮您写一个不用的 export default connect(state => ({ user: state.user, }))(User);

dva的model 注意点:

reducers 命名需正确,注意加s effects

子组件显示父组件穿过来的props有两种方式:

1、直接使用 这种方式,父组件改变props后,子组件重新渲染,由于直接使用的props,所以我们不需要做什么就可以正常显示最新的props

class Child extends Component { render() { return {this.props.someThings} } } 2、转换成自己的state 这种方式,由于我们使用的是state,所以每当父组件每次重新传递props时,我们需要重新处理下,将props转换成自己的state,这里就用到了 componentWillReceiveProps。

关于你提到的不会二次渲染是这样的:每次子组件接收到新的props,都会重新渲染一次,除非你做了处理来阻止(比如使用:shouldComponentUpdate),但是你可以在这次渲染前,根据新的props更新state,更新state也会触发一次重新渲染,但react不会这么傻,所以只会渲染一次,这对应用的性能是有利的。

class Child extends Component { constructor(props) { super(props); this.state = { someThings: props.someThings }; } componentWillReceiveProps(nextProps) { this.setState({someThings: nextProps.someThings}); } render() { return {this.state.someThings} } }

react jsx语法中如何遍历对象能同时拿到对象中的key和value?

const obj = {‘a’: 1, ‘b’: 2}

Object.keys(obj); //[“a”, “b”]

Object.values(obj); //[1, 2]

Object.keys(obj).forEach(key => console.log(key, obj[key])); //a 1 // b 2

yield call put 的使用

call( fuc, arg )

父组件 需要 setState 组件才会 传递异步的 数据到 props

前后端路由差别 1.后端每次路由请求都是重新访问服务器 2.前端路由实际上只是JS根据URL来操作DOM元素, 根据每个页面需要的去服务端请求数据,返回数据后和模板进行组合。