import
的资源,因此:
defaultValue
给出默认值;import React from 'react';
{}
相似于eval()、模板引擎、JS``模板变量,先执行
(执行模板中的表达式或调用)再渲染
(将表达式运行的结果渲染出来),因此要特别注意{}
中须要的是函数
仍是函数调用()
。npm install
时要注意包的使用环境。 Failed to compile
Module not found: Can't resolve 'XXX' in 'xxx.lib'
Functions are not valid as a React child. This may happen if you return a Component instead of <Component /> from render. Or maybe you meant to call this function rather than return it.
函数做为React子函数无效。 若是从render props中返回{Component}
而不是<Component />
,则可能会发生这种状况。 或许你打算调用这个函数而不是返回它。React.Children.only expected to receive a single React element child.
,某个组件能够经过PropTypes做类型检查,限制属性类型MyComponent.propTypes = {
children: PropTypes.element.isRequired
};
复制代码
redux
中使用combineReducers
时要注意其实现原理 —— 相似于以下代码: 通常实现:const chatReducer = (state = defaultState, action = {}) => {
return {
chatLog: chatLog(state.chatLog, action),
statusMessage: statusMessage(state.statusMessage, action),
userName: userName(state.userName, action)
}
};
复制代码
combineReducers实现const combineReducers = reducers => {
return (state = {}, action) => {
return Object.keys(reducers).reduce(
(nextState, key) => {
nextState[key] = reducers[key](state[key], action);
return nextState;
},
{}
);
};
};
复制代码
不管是针对哪一个属性拆分出来的子reducer
,在任意dispatch
发生时,都会被执行,因此定义subReducer
时要注意在内部根据action.type
来判断执行路径,若是这种type
的action
不会影响subReducer
对应的state.key
,那么必定要直接返回该state.key
。