将包装器中的道具传递到一个子页面

时间:2017-12-16 13:40:20

标签: reactjs react-router react-props

您好,并提前感谢您的帮助。我有一个问题将道具传递给加载路由的组件。我有一个带有包装器组件的路由文件,该组件可以加载有关路径URL的页面。在包装器组件(Layout)上我想向子组件传递一些道具。但是当使用this.props.children调用子组件时,我不知道如何传递道具。我尝试了很多东西,没有任何效果。

我有以下rotes文件:

import React from 'react';
import { Route, IndexRoute } from 'react-router';
import Layout from '../components/pages/Layout.js';
import Search from '../components/pages/Search.js';
import Queue from '../components/pages/Queue.js';
import About from '../components/pages/About.js';

const routes = () =>
    <Route path="/" component={Layout}>
        <IndexRoute component={Search}></IndexRoute>
        <Route path="queue" component={Queue}></Route>
        <Route path="about" component={About}></Route>
    </Route>
export default routes;

在布局中我有:

import React from "react";

import Footer from "../common/Footer.js";
import Nav from "../common/Nav.js";
import Header from "../common/Header.js";

export default class Layout extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            isSongPlaying: false,
            playingTrackId: "",
            playingList: []
        }
    }

    handleClickTrack(track) {
        this.setState({
           isSongPlaying: !this.state.isSongPlaying
        });

    }

    renderTrack(i) {
        return (
            <Player audio_id={id} />
        );
    }

    render() {


        const { location } = this.props;
        const { history } = this.props;
        const { children } = this.props;

        return (
            <div>
                <Header />
                <Nav location={location} history={history}/>

                <div className="container">
                    <div className="row">
                        <div className="col-lg-12">
                            {this.props.children}
                        </div>
                    </div>
                    <div className="row">
                        <div className="col-lg-12">
                            <div className="song-player">
                                {this.state.isSongPlaying ? this.renderTrack(this.state.playingTrackId) : null}
                            </div>
                        </div>
                    </div>
                    <Footer/>
                </div>
            </div>

        );
    }
}

在{this.props.children}上,组件正在加载我的页面组件Search,Queue和About,但我想在我的Search和Queue组件中添加回调道具。

在我的包装器布局组件上,我想实现以下目的:

从“反应”中导入React;

import Footer from "../common/Footer.js";
import Nav from "../common/Nav.js";
import Header from "../common/Header.js";

export default class Layout extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            isSongPlaying: false,
            playingTrackId: "",
            playingList: []
        }
    }

    handleClickTrack(track) {
        this.setState({
           isSongPlaying: !this.state.isSongPlaying
        });

    }

    renderTrack(i) {
        return (
            <Player audio_id={id} />
        );
    }

    render() {


        const { location } = this.props;
        const { history } = this.props;
        const { children } = this.props;

        return (
            <div>
                <Header />
                <Nav location={location} history={history}/>

                <div className="container">
                    <div className="row">
                        <div className="col-lg-12">
                            {RENDER SEARCH WITH onClick prop}
                            {RENDER QUEUE WITH onClick prop}
                        </div>
                    </div>
                    <div className="row">
                        <div className="col-lg-12">
                            <div className="song-player">
                                {this.state.isSongPlaying ? this.renderTrack(this.state.playingTrackId) : null}
                            </div>
                        </div>
                    </div>
                    <Footer/>
                </div>
            </div>
        );
    }
}

2 个答案:

答案 0 :(得分:0)

我在我的React应用程序中使用render={() => <Component/>}来提供我的Routes道具。不知道这是不是最完美的方式。可能还有其他方法。但它正在发挥作用! :)

以下是其中一条路线的示例:

<Route exact path="/queue" render={() => <Queue prop={something}/>} />

答案 1 :(得分:0)

您可以使用childContextTypes静态对象将道具传递给子组件。在父布局组件中的上下文中定义。

static childContextTypes={
      isSongPlaying: React.PropTypes.bool,
        playingTrackId:React.PropTypes.string,
        playingList: React.PropTypes.array

 }

然后使用Layout类

中的getChildContext()填充值
  getChildContext=()=>{
   return {
   isSongPlaying: false,
    playingTrackId:"Any Value to child component that you are going to pass",
    playingList: [] //Array with value

  }
  }

现在,您可以通过定义上下文类型来获取子组件(About.jsx或Search.jsx)中的值

static contextTypes={
   isSongPlaying: React.PropTypes.bool,
    playingTrackId:React.PropTypes.string,
    playingList: React.PropTypes.array

}

现在,您可以使用下面的上下文

访问子组件中的属性值
    let isPlaying= this.context.isSongPlaying  //or 
    let playingTrackId=this.context.playingTrackId