admin管理员组文章数量:1317909
How do I play a sound on the /start-sound and stop the sound on the /stop-sound?
/start-sound
const history = useHistory();
const startSound = () => {
const sound = new Audio("test.mp3");
sound.play();
history.push("/stop-sound", [sound]);
}
/stop-sound
const stopSound = () => {
sound.pause();
sound.currentTime = 0;
}
This code will display an error in the browser.
DataCloneError: Failed to execute 'pushState' on 'History': An object could not be cloned.
history.push("/stop-sound", [sound]);
This style will follow the React Router documentation.
React Router | history
And I don't know how to use sound object with /stop-sound.
Another example
demo
/pass-text
const PassTextPage = () => {
const history = useHistory();
const passText= () => {
history.push({
pathname: "/view-text",
state: { name: "Hello" }
});
};
return <button onClick={passText}>pass</button>;
};
/view-text
const ViewTextPage = () => {
const text = props.location.state.name; // 'props' is not defined.
const viewText = () => {
console.log(text);
};
return <button onClick={viewText}>view</button>;
};
App.jsx
const App = () => (
<BrowserRouter>
<Route exact path="/">
<Home />
</Route>
<Route exact path="/pass-text">
<PassTextPage />
</Route>
<Route exact path="/view-text">
<ViewTextPage />
</Route>
</BrowserRouter>
);
How do I play a sound on the /start-sound and stop the sound on the /stop-sound?
/start-sound
const history = useHistory();
const startSound = () => {
const sound = new Audio("test.mp3");
sound.play();
history.push("/stop-sound", [sound]);
}
/stop-sound
const stopSound = () => {
sound.pause();
sound.currentTime = 0;
}
This code will display an error in the browser.
DataCloneError: Failed to execute 'pushState' on 'History': An object could not be cloned.
history.push("/stop-sound", [sound]);
This style will follow the React Router documentation.
React Router | history
And I don't know how to use sound object with /stop-sound.
Another example
demo
/pass-text
const PassTextPage = () => {
const history = useHistory();
const passText= () => {
history.push({
pathname: "/view-text",
state: { name: "Hello" }
});
};
return <button onClick={passText}>pass</button>;
};
/view-text
const ViewTextPage = () => {
const text = props.location.state.name; // 'props' is not defined.
const viewText = () => {
console.log(text);
};
return <button onClick={viewText}>view</button>;
};
App.jsx
const App = () => (
<BrowserRouter>
<Route exact path="/">
<Home />
</Route>
<Route exact path="/pass-text">
<PassTextPage />
</Route>
<Route exact path="/view-text">
<ViewTextPage />
</Route>
</BrowserRouter>
);
Share
Improve this question
edited Dec 24, 2019 at 16:11
Jay White
asked Dec 24, 2019 at 8:46
Jay WhiteJay White
3152 gold badges3 silver badges12 bronze badges
9
- include props in const ViewTextPage = (props) – kumaran Commented Dec 24, 2019 at 13:58
- I got the error "TypeError: Cannot read property 'location' of undefined". – Jay White Commented Dec 24, 2019 at 14:20
- Post the parent ponent of viewTextPage ponent – kumaran Commented Dec 24, 2019 at 14:37
- Added parent ponents for two ponents. – Jay White Commented Dec 24, 2019 at 14:42
- 1 Change your parent ponent something like this <Route exact path="/view-text" ponent={ViewTextPage} /> – kumaran Commented Dec 25, 2019 at 4:05
1 Answer
Reset to default 7The way you can pass property to navigated ponent
history.push({
pathname: '/stop-sound',
state: { name: 'Hello'}
});
access like this in navigated ponent.
if its stateless ponent
let location = useLocation();
location.state.name
if class based ponent
this.props.location.state.name
本文标签: javascriptHow to pass object with historypush in reactrouter v5Stack Overflow
版权声明:本文标题:javascript - How to pass object with history.push in react-router v5? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742027501a2415822.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论