Javascript 如何将状态/对象传递给子例程(react router redux)?

Javascript 如何将状态/对象传递给子例程(react router redux)?,javascript,reactjs,redux,react-router,react-jsx,Javascript,Reactjs,Redux,React Router,React Jsx,我的“store.js”执行以下操作: export default function configureStore(initialState = {todos: [], floatBar: {} }) { return finalCreateStore(rootReducer, initialState) } <Link to={`/newpage/${this.props.todo.id}`}>Link1</Link> 然后在我的“client.js”中,我有初

我的“store.js”执行以下操作:

export default function configureStore(initialState = {todos: [], floatBar: {} }) {
 return finalCreateStore(rootReducer, initialState)
}
<Link to={`/newpage/${this.props.todo.id}`}>Link1</Link>
然后在我的“client.js”中,我有初始化状态,但没有定义“todos”数组,也没有设置路由器:

let initialState = {
  floatBar: {
    barStatus: false,
    id: 0
  }
}

let store = configureStore(initialState)

render(
  <div>
    <Provider store={store}>
      <Router history={hashHistory}>
        <Route
          path="/"
          component={App}
        >
        <Route
          component={FirstPage}
          path="firstpage"
        />
        <Route
          component={NewPage}
          path="/newpage/:id"
        />
        </Route>
      </Router>
    </Provider>
  </div>,
  document.getElementById('app')
)
您可以从props.params.id访问“todo id”。您还可以通过“mapStateToProps”中的“ownProps”访问NewPage的props.params

从“react redux”导入{connect};
从“React”导入React,{Component}
从“材质ui”导入{Divider}
常量样式={
标题:{
颜色:'白色',
textAlign:'左',
保证金左:30
}
}
类NewPage扩展组件{
render(){
返回(
{this.props.todo.title}

{this.props.todo.detail}

) } } const mapStateToProps=(state,ownProps)=>{ 让todo=state.todos.filter(todo=>todo.id==ownProps.params.id); 返回{ 待办事项:待办事项[0] }}; 导出默认连接(MapStateTops)(新建页面);
  • 另一种方法,对于动态道具特别有用,就是克隆作为一个组件注入的子组件 React Router提供的道具,让您有机会在此过程中传递其他道具。 范例
  • 类Repos扩展组件{
    构造函数(){…}
    componentDidMount(){…}
    render(){
    让repos=this.state.repositories.map((repo)=>(
    
  • {repo.name}
  • )); 让child=this.props.children&&React.cloneElement(this.props.children, {repositories:this.state.repositories} ); 返回( Github回购协议
      {repos}
    {child} ); } } //////////////////////////// 类RepoDetails扩展组件{ 渲染还原(){ 让repository=this.props.repositories.find((repo)=>repo.name==this.props.params。 回购名称); 让星星=[]; 对于(var i=0;i {星星} ); } render(){ 如果(this.props.repositories.length>0){ 返回此.renderepository(); }否则{ 返回装载。。。; } }
    }
    很抱歉,“让{text,title}=this.props.todo;”将this.props.todo.text设置为text,将this.props.todo.title设置为title?您能澄清一下();正在做什么?我是否应该修改“const-mapStateToProps”中的任何内容?1.是的,这是解构作业es6.2,我们正在传递道具。3.我可以编辑它,我已经读到你将todo对象传递给“Item”组件谢谢你的澄清。至于第2点,NewPage.js不应该从Item.js接收道具吗3,如果可以的话,我将不胜感激。我仍在努力解决这个问题。只是想确认一下,NewPage.js正在尝试从item.js接收todo对象。因此NewPage.js可以在item.js中使用'this.props.todo.text'和'this.props.todo.title'。JoKo item应该是NewPage的子对象。不是吗?item应该接收'todo'对象吗?只是为了澄清一下。我如果是的,那么上面的代码适合您认为这是一个误解。item.js是FirstPage.js的子对象,但是NewPage.js需要从item.js接收“todo”对象。在item.js中,定义了Link1。但是在NewPage.js中,我只想使用item.js的“this.props.todo.title this.props.todo.text”。
    import * as actions from '../redux/actions'
    
    class NewPage extends Component{
      handleCommentChange(){
        this.props.actions.updateComment()
      }
    
      render(){
        return()
      }
    }
    
    function mapDispatchToProps(dispatch){
      return{
        actions: bindActionCreators(actions, dispatch)
      }
    }
    
    export default connect(
      mapDispatchToProps
    )(NewPage);
    
    import {connect} from "react-redux";
    import React, { Component } from 'react'
    import { Divider } from 'material-ui'
    
    const styles = {
      title:{
        color: 'white',
        textAlign: 'left',
        marginLeft: 30
      }
    }
    
    class NewPage extends Component{
      render(){
        return(
          <div>
            &nbsp;
            <div style={styles.title}>
              <font size="4">
                {this.props.todo.title}
              </font>
            </div>
            &nbsp;
              <Divider style={{backgroundColor:'#282828'}}/>
            &nbsp;
            <p style={{color: 'white'}}>{this.props.todo.detail}</p>
          </div>
        )
      }
    }
    const mapStateToProps=(state, ownProps)=>{
    let todo = state.todos.filter(todo=>todo.id==ownProps.params.id);
    return{
        todo:todo[0]
    }};
    export default connect(mapStateToProps)(NewPage);