Reactjs 无法在React redux中使用connect访问存储
我正在应用程序中使用React。我正在使用connect访问商店,简单地说,我有以下代码:Reactjs 无法在React redux中使用connect访问存储,reactjs,redux,react-redux,Reactjs,Redux,React Redux,我正在应用程序中使用React。我正在使用connect访问商店,简单地说,我有以下代码: class MyComponent extends Component{ constructor(props){ super(props) } render(){ let components = this.props.components; if(components.indexOf("SomeString")){ /
class MyComponent extends Component{
constructor(props){
super(props)
}
render(){
let components = this.props.components;
if(components.indexOf("SomeString")){
//some stuffs
}
return (
<SomeElement/>
)
}
}
const mapStateToProps = state => {
return {
components: state.someReducer.components
}
}
export default connect(mapStateToProps)(MyComponent)
那么,我错过了什么?我尝试了很多没有结果的东西你需要检查
这个.props.components
像这样:
render(){
if(this.props.components) {
let components = this.props.components;
if(components.indexOf("SomeString")){
//some stuffs
}
return (
<SomeElement/>
)
} else {
return(<div>Loading...</div>)
}
}
render(){
if(本道具组件){
让components=this.props.components;
if(components.indexOf(“SomeString”)){
//一些东西
}
返回(
)
}否则{
返回(加载…)
}
}
您需要检查此.props.components
,如下所示:
render(){
if(this.props.components) {
let components = this.props.components;
if(components.indexOf("SomeString")){
//some stuffs
}
return (
<SomeElement/>
)
} else {
return(<div>Loading...</div>)
}
}
render(){
if(本道具组件){
让components=this.props.components;
if(components.indexOf(“SomeString”)){
//一些东西
}
返回(
)
}否则{
返回(加载…)
}
}
在第一个循环中,此.props.components
未定义。您可以使用
render(){
if(this.props.components) {
let components = this.props.components;
if(components!=null || components!=undefined){ //this will check if components is null or undefined
if(components.indexOf("SomeString")){
//some stuffs
}}
return (
<SomeElement/>
)
}
render(){
if(本道具组件){
让components=this.props.components;
如果(components!=null | | components!=undefined){//这将检查组件是否为null或未定义
if(components.indexOf(“SomeString”)){
//一些东西
}}
返回(
)
}
在第一个循环中,此.props.components
未定义。您可以使用
render(){
if(this.props.components) {
let components = this.props.components;
if(components!=null || components!=undefined){ //this will check if components is null or undefined
if(components.indexOf("SomeString")){
//some stuffs
}}
return (
<SomeElement/>
)
}
render(){
if(本道具组件){
让components=this.props.components;
如果(components!=null | | components!=undefined){//这将检查组件是否为null或未定义
if(components.indexOf(“SomeString”)){
//一些东西
}}
返回(
)
}
您是否为someReducer
reducer定义了初始状态?@elas是的,事实上,当我在控制台中打印数组时,我看到了值,并且它们被更正了。我可以确定,由于某种原因,组件未定义,问题不在于您提供的代码段。可能在更高的位置p、 正如其他人已经说过的,在呈现结果之前提供一个检查。你是否定义了someReducer
reducer的初始状态?@elas是的,事实上,当我在控制台中打印数组时,我看到了值,它们被更正了。我可以确定,由于某种原因,组件未定义,问题不会出现ie在您提供的代码片段中..可能在更高的位置。正如其他人已经说过的,在呈现resultok之前提供一个检查,但现在我的问题是:为什么数组在控制台中打印得很好?在第一个循环中,您将console.log?render()放在了哪里是react生命周期中第一个被调用的部分。我在renderok中调用了它,但现在我的问题是:为什么数组在控制台中打印得很好?在第一个周期中,您将console.log放在了哪里?.render()是react生命周期中第一个被调用的部分。我在render中调用了它