记录学习React时自己是如何挖坑把自己埋了的过程:children以及其它props被修改时相关组件无法重新渲染(做了两天)

父组件代码:

class UserHome extends Component<Props, State> implements IUserHome {

  public name: string | undefined;

  public readonly state: State = initialState;

  public handlerClick() {
store.dispatch(sendAction());
} /**
* DOM挂载完成后执行
*/
public componentDidMount() {
store.subscribe(this.subscribe)
} public render() {
return (
<HomeBackground url={BackgroundImg}>
<HomeScreenHeightBox width={1400} background='rgba(246, 248, 249, .92)'>
<button onClick={() => {
this.props.setCount(10)
// console.log(this.state.count)
}}> 点我</button>
<HomeNavigationBar height={this.state.count} />
</HomeScreenHeightBox>
</HomeBackground>
)
}
} const mapDispatchToProps = (dispatch: Function): mapDispatchToPropsInterface => {
return {
sendAction() {
dispatch({
type: 'send_action',
value: "UpYou of blog"
})
},
setCount(sun) {
dispatch({
type: 'set_count',
count: sun
})
}
}
} const mapStateToProps = (state: StateInferface) => {
// console.log(state)
return state;
} export default connect(mapStateToProps, mapDispatchToProps)(UserHome)

readux层

const initialState: StateInferface = {
value: "HELLO WORLD",
count: 50,
height: 50
} const reducer = (state = initialState, action: ActionInterface): any => {
switch (action.type) {
case "send_action":
return Object.assign({ ...state }, action);
case "set_count":
return Object.assign({ ...state }, { count: state.count + 10});
default:
return state;
}
} export default reducer;

现需求是点击“点我”按钮改变HomeNavigationBar组件height属性,HomeNavigationBar代码(这个组件实际上是将height再传给另一个容器组件NavigationBar,一下省略了中间调用代码):

/**
* 首页导航栏容器
*/
export default class HomeNavigationBar extends PureComponent<Props, object> implements IHomeNavigationBar { // 设置默认Props
static defaultProps = {
height: 50,
backColor: "#3a3f51"
} private height?: number = this.props.height; // 导航栏高度
private backColor?: string = this.props.backColor; // 背景颜色
private children?: ReactNode = this.props.children; // 插槽 render() {
const homeNavigationBarStyles = {
height: `${this.height}px`,
backgroundColor: `${this.backColor}`
}
return (
<div id='HomeNavigationBar' style={homeNavigationBarStyles}>
{this.props.height}
<div className="flex-1">
{this.children}
</div>
</div>
);
}
}

NavigationBar中有几段代码导致无法动态改变、重新渲染组件

为了方便直接将props中的值给到height字段,来简单验证一下图中圈起部分代码的可行性:

上图证明无法对基础数据类型的数据进行修改,而是直接将当前变量中的内存地址替换:

let a = 0x213; // 例如等于3
a = 2;// 0x645 改变a的值,实际上改变的是内存地址

所以当props改变时height数据没变化就是这个原因,需要将style中的this.height改为this.props.height,声明的其它变量作废...

由于react中state或props改变时就会触发render方法重新渲染DOM,可以在render中定义height等字段const { children, height, backColor } = this.props;

render() {
const { children, height, backColor } = this.props;
const homeNavigationBarStyles = {
height: `${height}px`,
backgroundColor: `${backColor}`
}
return (
<div id='HomeNavigationBar' style={homeNavigationBarStyles}>
<div className="flex-1">
{children}
</div>
</div>
);
}

React react-redux props或state更新视图无法重新渲染问题的更多相关文章

  1. A Bite Of React(2) Component, Props and State

    component component:用户自己定义的元素 const element = <Welcome name="Sara" />; class Welcome ...

  2. React 深入系列3:Props 和 State

    文:徐超,<React进阶之路>作者 授权发布,转载请注明作者及出处 React 深入系列3:Props 和 State React 深入系列,深入讲解了React中的重点概念.特性和模式 ...

  3. React中Props 和 State用法

    React中Props 和 State用法 1.本质 一句话概括,props 是组件对外的接口,state 是组件对内的接口.组件内可以引用其他组件,组件之间的引用形成了一个树状结构(组件树),如果下 ...

  4. 前端(十一):props、state及redux关系梳理

    所谓状态机,是一种抽象的数据模型,是“事物发展的趋势”,其原理是事件驱动.广泛地讲,世界万物都是状态机. 一.状态机是一种抽象的数据模型 在react中,props和state都可以用来传递数据.这里 ...

  5. react native中对props和state的理解

    最近使用react native这个新的技术做完一个项目,所以赶紧写个博客巩固一下. 今天我想说的是props和state,当然这是我个人的理解,如果有什么不对的地方,望指正. 首先我先说说props ...

  6. react~props和state的介绍与使用

    props是参数的传递,从上层模块向下层模块进行拿传递:而state是提局域变量,一般在本模块内使用,props是不能改变的,而state可以通过setState去修改自身的值. props Reac ...

  7. React中props和state相同点和不同点

    朋友们,我想死你们了,最近这几天忙着和病魔作斗争所以没怎么写博客,今天感觉好点了,赶紧来写一波,就是这木敬业. 今天我们来讨论讨论props和state相同点和不同点 首先我来概要说明一下这两者 pr ...

  8. React Native 快速入门之认识Props和State

    眼下React Native(以后简称RN)越来越火,我也要投入到学习当中.对于一个前端来说,还是有些难度.因为本人觉得这是一个App开发的领域,自然是不同.编写本文的时候,RN的版本为0.21.0. ...

  9. React Native中组件的props和state

    一.组件的属性(props)和状态(state) 1.属性(props) 它是组件的不可变属性(组件自己不可以自己修改props). 组件自身定义了一组props作为对外提供的接口,展示一个组件时只需 ...

随机推荐

  1. Effective C++ 读书笔记 名博客

    https://www.cnblogs.com/harlanc/tag/effective%20c%2B%2B/default.html?page=3

  2. 【保姆级教程】手把手教你进行Go语言环境安装及相关VSCode配置

    [Go语言入门系列]前面的文章: [Go语言入门系列](七)如何使用Go的方法? [Go语言入门系列](八)Go语言是不是面向对象语言? [Go语言入门系列](九)写这些就是为了搞懂怎么用接口 本篇文 ...

  3. 01 How does C Programming work ? C语言如何工作?

    where is C used ? C 语言的应用场景 C is widely used C语言被广泛应用于: For creating desktop applications 用于创建桌面应用程序 ...

  4. linux 中 eclipse 开发 c/c++ 多线程程序,添加 libpthread.a 库支持

    导入头文件 在 linux 中开发多线程程序,在使用到 pthread 系列函数的文件中,需要导入头文件: #include <pthread.h> 链接 libpthread.a 在编译 ...

  5. 微信小程序 audio组件 默认控件 无法隐藏/一直显示/改了controls=‘false’也没用2019/5/28

    <audio>默认控件,如果需要隐藏,不需要特意设置controls = 'false',直接把这个属性删除即可,不然无论如何都会存在 之前,设置了controls = 'false' & ...

  6. linux centos 04

    1.python的虚拟环境 1.将当前机器上的解释器作为一个 本地,复制出的很多歌 虚拟解释器 物理机上的 本体解释器 ,什么事也不做 分身1:  解释器1:虚拟环境1     运行django  1 ...

  7. day18 Pyhton学习 匿名函数

    匿名函数 别称: lambda表达式 函数,没有名字 def wahaha(n):#wahaha return n**2 print(wahaha.__name__) qqxing = lambda ...

  8. Mysql数据库分布式事务XA详解

    XA事务简介 XA 事务的基础是两阶段提交协议.需要有一个事务协调者来保证所有的事务参与者都完成了准备工作(第一阶段).如果协调者收到所有参与者都准备好的消息,就会通知所有的事务都可以提交了(第二阶段 ...

  9. centos8安装RabbitMQ

    一.安装erlang # 添加仓库 curl -s https://packagecloud.io/install/repositories/rabbitmq/erlang/script.rpm.sh ...

  10. docker系统化学习图文教程

    1.背景 在实际开发中我们经常遇到这样的情况: 1.开发的时候测试好的程序已发布到线上就出问题: 2.线上的集群环境需要扩容时非常麻烦,比如说要装jdk.mysql.redis等,如果扩容100台服务 ...