javascript - 为 react 组件设置默认 Prop

标签 javascript reactjs ecmascript-6 redux

我希望我的应用程序组件有一个默认 Prop ,并在 mapStateToProps 中使用该 Prop 。

容器/App.js

import React, { Component, PropTypes } from 'react';
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import Footer from '../components/Footer';
import TreeNode from '../containers/TreeNode';
import Home from '../containers/Home';
import * as NodeActions from '../actions/NodeActions'

export default class App extends Component {

  constructor(props, context) {
    super(props, context)
    this.props = {
      info:{
        path:'/'
      }
    }
  }

  componentWillMount() {
    // this will update the nodes on state
    this.props.actions.openNode('/');
  }

  render() {
    const { node , actions} = this.props
    console.log(this.props)

    return (
      <div className="main-app-container">
        <Home />
        <div className="main-app-nav">Simple Redux Boilerplate</div>
        {node.childNodes &&
          <div>{node.childNodes.map(node => <TreeNode key={info.path} info={node} tree={tree} actions={actions} />)}</div>
        }
        {!node.childNodes &&
          <div>no children</div>
        }
        {/*<Footer />*/}
      </div>
    );
  }
}

App.defaultProps = {
    info:{
      path:'/'
    }
};

function mapStateToProps(state, ownProps) {
  console.log('state')
  console.log(state)
  console.log('ownProps')
  console.log(ownProps)
  return {
    node: ownProps.info.path? state.TreeNodeReducer.tree[ownProps.info.path] : {}
  };
}


function mapDispatchToProps(dispatch) {
  return {
    actions: bindActionCreators(NodeActions, dispatch)
  };
}

export default connect(
  mapStateToProps,
  mapDispatchToProps
)(App);

reducers/TreeNodeReducer.js

import { OPEN_NODE, CLOSE_NODE, GET_NODES } from '../constants/NodeActionTypes';
import UUID from "node-uuid"

const initialState = {
  open: false,
  info: {} 
}

class NodeModel {
    constructor(path, type, right) {
        this.name = path;
        this.path = path;
        this.type = type;
        this.right = right;
    }
}

let lastId = 0;

function getFileList() {
  var testNodes = []
  for (var i=0; i< 3; i++) {
    testNodes.push(new NodeModel(lastId,'d', i.toString()))
    lastId++;
  }

  return testNodes
}


const getNodes = (state, action) => {
  var { path } = action
  var tree = state.tree ? state.tree : {}
  tree[path] = getFileList(path)
  return {
    ...state,
    tree:tree
  };
};

export default function (state = initialState, action) {
  switch (action.type) {
    case OPEN_NODE:
      return { ...getNodes(state, action), open:true };
    case GET_NODES:
      return getNodes(state, action);
    case CLOSE_NODE:
      return {
        ...state,
        open:false
      };
    default:
      return state;
  }
}

我试过了:

1: 构造函数

  constructor(props, context) {
    super(props, context)
    this.props = {
      info:{
        path:'/'
      }
    }
  }

2: defaultProps:

App.defaultProps = {
    info:{
      path:'/'
    }
};

但是没有一个有效... 日志总是:

enter image description here

state 具有正确的默认值,但 ownProps 为空...

PS:项目是here .

最佳答案

我找到了一个更好的方法来设置 defaultProps,

因为 connect() 返回一个新组件。如果你想让 defaultProps 出现在它上面,你需要把 defaultProps 放在它上面:

App = connect(
  mapStateToProps,
  mapDispatchToProps
)(App);

App.defaultProps = {
  path:'/'
};

export default App

这行得通!

关于javascript - 为 react 组件设置默认 Prop ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38004703/

相关文章:

javascript - 意外的标记 { 语法错误

JavaScript 随机数组

javascript - 衡量 Javascript 性能

javascript - img.onload 在 new Image() 声明后拒绝触发

reactjs - Material-UI:如何解决 "Warning: Prop ` id` 不匹配”

reactjs - es6 React在子组件中获取 Prop

javascript - ES6 : Emitting events from static methods

javascript - Webpack 生产版本

javascript - 在通过标记数组映射之前,reactjs nullcheck

javascript - 在 React-Router Children 中渲染 Ajax 请求