简体   繁体   English

将一些东西传递给React / Redux中的主app.js文件

[英]Passing something to main app.js file in React/Redux

Is ther a any way to pass redux state to main app.js file in react/redux? 有什么办法可以将redux状态传递给react / redux中的主app.js文件吗? I wan't to change classname based on redux state. 我不会根据redux状态更改类名。 I tried to combine even with local storage but still can't make a satisfying solution. 我什至尝试与本地存储结合使用,但仍然无法提供令人满意的解决方案。

This is my main app.js file 这是我的主要app.js文件

class App extends Component {

    render() {
        return (

            <Provider store={store}>
                <Router>
                    <div style={{display: 'flex'}} >
                        <Navbar/>
                        <Switch>
                            <Route component={UserIsAuthenticated(MainProfile)} exact path="/profile"/>
                            <Route component={UserIsAuthenticated(MyDiet)} exact path="/diet"/>
                            <Route component={UserIsAuthenticated(AllDiets)} exact path="/alldiets"/>
                            <Route component={UserIsAuthenticated(EditProfile)} exact path="/editprofile"/>
                            <Route component={UserIsAuthenticated(WorkoutPlan)} exact path="/workout"/>
                            <Route component={UserIsNotAuthenticated(Login)} exact path="/login"/>
                            <Route component={UserIsAuthenticated(EditDiet)} exact path="/edit/:id"/>
                            <Route component={UserIsAuthenticated(EditDiet)} exact path="/edit/:id"/>
                            {/*<Route component={UserIsAuthenticated(UsersDiet)} exact path="/usersdiet"/>*/}
                        </Switch>
                    </div>
                </Router>
            </Provider>
        )
    }
}

export default (App)

You're already using store using provider in the app. 您已经在应用中使用提供者使用商店。 So, you can use: 因此,您可以使用:

store.getState().redux_state_name

If you need the redux state in the component that is wrapped in your Provider then you might consider moving the Provider component to where you initially render your React. 如果您需要在Provider包装的组件中使用redux状态,则可以考虑将Provider组件移动到最初呈现React的位置。

entry.jsx entry.jsx

const store = createStore(...);

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

app.jsx app.jsx

class App extends Component {

    render() {
        return (
            <div className={`${this.state.someData ? 'someClassA' : 'someClassB'}`} >
                <Navbar/>
                <Switch>
                    <Route>'s...  
                </Switch>
            </div>
        );
    }
}

const mapStateToProps = state => {
  return {
    someData: state.foo
  }
}

export default connect(
    mapStateToProps,
    null
)(App)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM