Open violet0sea opened 6 years ago
@v16.3(.0-alpha)
1.context Api 之前一直存在 用于父组件向深层次的子组件传递数据 之前一直不使用是因为api可能会在将来发生改变 可以在不使用redux和mobx的情况下管理简单的状态 使用新的createContext方法返回两个组件,Provider和Consumer
import { createContext } from "react"; const CustomContext = createContext({ //data here 默认的数据 如果在Provider里面没有指定的话就使用当前数据 name: 'cc ' });
如何使用,Redux的小翻版, provider应该是外层用于包装数据的 render() { <CustomContext.Provider value={{name: 'hai'}}>
<Main />
<Footer />
</CustomContext.Provider> }
consumer 是内部组件使用数据的 const Header = () => (
React key point:
make React develop gracefully
const Btn = ({className, primary, ...props}) => <Button type="button" className={classnames( "btn", primary && "btn-primary", className )} {...props} /> material-ui里的primary 和 secondary控制按钮样式的实现应该也是同理, class控制样式 样式分离 也可以使用style控制 逊色许多 内嵌的样式让显示变得复杂多变
const Connect = ComposedComponent => class extends Component { constructor() { super(); this.state = { name: '' }; }
componentDidMount() {
this.setState({
name: 'Lina'
});
}
render() {
return (
<ComposedComponent
{...this.props}
name={this.state.name}
/>
);
}
}
const ConnectedMyComponent = Connect(Greeting);` Connect accept a compont and return a new wrapped component,which can receive new props to change the behavior
关于props传递数据的认知: props传递数据一般情况下可以传js的基本数据类型, 其次可以传递回调函数,主要用于状态提升后的数据更新同步 还有就是可以传递组件
controlled/uncontrolled component most time use controlled component,you should use state and handler to update value when using uncontrolled component, you can use ref to access the value
问题: hooks写的组件类似一个封闭的生态系统,状态如何传递出去?原本想着从外部拿到的状态仅作为初始化,传递通过props回调函数实现,但是存在一个问题,回调函数怎么触发?业务场景下是hooks组件平级的下一步组件触发,两个兄弟组件之间传递数据,传统的方法是状态提升,但是因为过于抽象的缘故,这个方法无法实现;另一个想法是组件下沉,让同级组件下沉到hooks组件内部触发事件的时候获取内部状态并传递到上一层
// 父组件
class MainComp extends Component {
handleData = data => {
// todo handle data
console.log(data);
};
render() {
return (
<div>
<HooksComp
render={sharedState => <FooterButtonGroup sharedState={sharedState} onClick={handleData} />}
></HooksComp>
</div>
);
}
}
// 底部控制步骤的按钮
class FooterButtonGroup extends Component {
handleClick = () => {
const { onClick, sharedState } = this.state;
onClick(sharedState);
};
render() {
<div>
<button>prev</button>
<button onClick={this.handleClick}>next</button>
</div>;
}
}
// hook组件
function HooksComp(props) {
const [state, setState] = useState({ a: 1 });
const { render } = props;
return (
<div>
<p>{state.a}</p>
<p>-----分割线-----</p>
{render(state)}
</div>
);
}
在HooksComp组件里使用useEffect方法,在里面调用从父组件传递的回调函数,并把需要的数据作为参数传递出去
class MainComp extends Component {
handleTransferData = data => {
// todo handle data
console.log(data);
};
render() {
return (
<div>
<HooksComp onTransferData={this.handleTransferData}></HooksComp>
</div>
);
}
}
function HooksComp(props) {
const [state, setState] = useState({ a: 1 });
const { handleTransferData } = props;
useEffect({
if (typeof handleTransferData === 'function') {
const {a,b,c, ...rest} = state;
handleTransferData({
a,
b,
})
}
}, [state.a, state.b]);
return (
<div>
<p>{state.a}</p>
<p>-----分割线-----</p>
{render(state)}
</div>
);
}
@v16.0 1.render方法可以返回不同于以往的数组元素和字符串 返回数组元素时需要添加key值 render() { return [