javascript - 如何使用 props 组件 React 发送 props

标签 javascript reactjs

现在,我正在尝试将 props 从 props 发送到组件,但我不能。我该怎么办?

有人可以告诉我如何将 props 组件传递给 props 组件吗 (聚焦 header.jsx 文件,我需要发送 props 中的值,但我不能

谢谢。

index.js

ReactDOM.render(
    <HashRouter>
        <Provider store={store}>
            <Route component={App}/>
        </Provider>
    </HashRouter>,
    document.getElementById('root')
);

serviceWorker.unregister();

app.jsx

render() {
    const { location, lang } = this.props
    return (
    <MuiThemeProvider theme={theme}>
        <IntlProvider key={lang} locale={lang} messages={messages[lang]}>
        <Layout>
                <Switch>
                    <GuestRoute location={location} path='/' component={Welcome} exact/>
                    <GuestRoute location={location} path='/reset/:token' component={ForgotPassword} exact/>
                    <UserRoute location={location} path='/profile' component={Info} exact/>
                    <UserRoute location={location} path='/reset' component={ResetPassword} exact/>
                    <UserRoute location={location} path='/myrestaurant' component={MyRestaurant} exact/>
                    <UserRoute location={location} path='/myrestaurant/:resname' component={MyRestaurant} exact/>
                </Switch>
        </Layout>
        </IntlProvider>
    </MuiThemeProvider>
    );
}

布局.jsx

class InfoPage extends Component {
    render() {
        const { isAuthenticated } = this.props
        return (
            <div>
                { 
                    isAuthenticated 
                        ?
                        <HeaderAuth 
                            component={this.props.children}
                        />
                        :
                    <HeaderNAuth />
                }
                { !isAuthenticated && this.props.children }
            </div>
        )
    }
}

header.jsx

    render() {
        const { classes, locale, theme, logout } = this.props

        return(
            <div className={classes.root}>

                    {
                        this.props.component <-------------- HERE I NEED PROPS VALUE TO THIS COMPONENT ----------------> // what should i do
                    }

            </div>
        )
    }
} 

最佳答案

在这里您可以在 HeaderAuth 中传递整个组件。

class InfoPage extends Component {
    render() {
        const { isAuthenticated } = this.props
        return (
            <div>
                { 
                    isAuthenticated 
                        ?
                        <HeaderAuth newValue={'hello world'}> 
                          {this.props.children}
                        <HeaderAuth/>
                        :
                    <HeaderNAuth />
                }
                { !isAuthenticated && this.props.children }
            </div>
        )
    }
}

现在,在 HeaderAuth 内部,您可以访问 this.props.children 内的该组件。

render() {
        const { classes, locale, theme, logout, newValue } = this.props

        return(
            <div className={classes.root}>
                {newValue}
                {this.props.children}
            </div>
        )
}

关于javascript - 如何使用 props 组件 React 发送 props,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54473017/

相关文章:

reactjs - 使用 Link 在 React-Router 中上一级

javascript - 对于我的井字板,有没有比 check4winner() 更好的函数

javascript - 网络错误 : Unexpected token < in JSON at position 0 at new ApolloError

javascript - 如何使用 JavaScript 隐藏文本区域

javascript - 在 React 中单击按钮时获取文本框值的最佳方法是什么?

javascript - 该插件文件同时使用 CommonJS 和 ES6 模块系统,我们不支持

reactjs - useRef 和 useEffect 初始访问,然后使用 useRef

javascript - 将 Kendo UI 下拉列表绑定(bind)到由数据源填充的 ViewModel 的最佳方法是什么?

javascript - 如何围绕其末端旋转 ThreeJS Object3D 对象?

reactjs - 在 useEffect 中响应条件