我无法弄清楚如何在this.props.user更改值时重新渲染我的componentsnet .最初的值为this.props.usernull,但它会在几秒后更改为实际值.下面我展示了儿童组件.父组件将商店状态映射到它的props,我将它传递给下面的子组件类.
export class UserInfoComponent extends Component {
constructor(props){
super(props);
this.state = {user: this.props.user}
}
componentWillReceiveProps(){
this.setState({user: this.props.user})
}
render() {
if(this.state.user)
return (
<h1>{this.state.user.firstName}</h1>
);
return (
<h1>loading</h1>
)
}
}
Run Code Online (Sandbox Code Playgroud) 我有两个字符串列表,我想按元素将它们连接起来以创建第三个列表
第三个列表应按list_1原样包含所有元素,并为每个可能的元素组合添加新元素list_1+list_2
请注意,两个列表的长度不一定相同
例:
base = ['url1.com/','url2.com/', 'url3.com/',...]
routes = ['route1', 'route2', ...]
urls = ['url1.com/' + 'url1.com/route1', 'url1.com/route2', 'url2.com/', 'url2.com/route1', 'url2.com/route2', ...]
Run Code Online (Sandbox Code Playgroud)
我尝试使用该zip方法,但未成功
urls = [b+r for b,r in zip(base,routes)]
Run Code Online (Sandbox Code Playgroud) 我有一个字符串列表,我想以下列方式连接列表的元素:
before = ['a', 'b', 'c', 'd']
after = ['ab', 'bc', 'cd']
我不确定如何调用上述操作.
但是,我尝试使用范围方法:
after = [before[i]+before[i+1] for i in range(0,len(before),2)]
但它导致: after = ['ab', 'cd']