Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
400 views
in Technique[技术] by (71.8m points)

reactjs - "Or" operator not working on window.location.pathname

GOAL: Don't show Navigation component when the pathname includes either /create-page or add-block

Currently, the Navigation shows on both pages

<Route
            path={[
              "/affiliate-code",
              "/dashboard",
              "/editor",
              "/locked",
              "/saved",
              "/notifications",
              "/:id",
              "/",
            ]}
            component={() =>
              window.location.pathname != ("/create-page" || "/add-block") && (
                <Navigation
                  key={window.location.pathname}
                  newNotification={newNotification}
                />
              )
            }
          />
question from:https://stackoverflow.com/questions/65924590/or-operator-not-working-on-window-location-pathname

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You cannot use an OR operator like so, you need to compare each value in the eval statement. If you want 1 statement I would suggest something like this:

!["/create-page","/add-block"].includes(window.location.pathname) && (
...
)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

Just Browsing Browsing

2.1m questions

2.1m answers

60 comments

56.9k users

...