reactjs Link not working,when render after ajax request - reactjs

I want render a table from ajax request data. Each row with a action link to enter a edit page.when I click the link, the url on browser was changed,but it can't enter edit page.
In addition, when I click link button, the url on browser is not I except. (It's http://192.168.1.186:4444/table/basic-table2/articles rather than http://192.168.1.186:4444/#/table/basic-table2/articles).
I'm thinking the problem maybe cause by lifecycle of component mounting. Because when I put Link element out side the table(not wait ajax request return), it work well.
I'm new in React, Can anyone finger out where I am wrong?following is the code:
renderOperator = (value, index, record) => {
const edit = <Link exact="true" to="/table/basic-table2/articles">编辑</Link>;
const context = {};
return (
<div>
<Router context={context}>
<span>
{edit}
</span>
</Router>
<a
style={styles.removeBtn}
onClick={this.deleteItem.bind(this, record)}
>
删除
</a>
</div>
);
};
fllowing is the table:
<Table
dataSource={this.state.dataSource}
isLoading={this.state.isLoading}
rowSelection={{
...this.rowSelection,
selectedRowKeys: this.state.selectedRowKeys,
}}
>
<Table.Column title="编码" dataIndex="article_id" width={120} />
<Table.Column title="名称" dataIndex="article_title" width={250} />
<Table.Column title="报名数" dataIndex="enrolled_num" width={160} />
<Table.Column title="访问人数" dataIndex="visited_user_num" width={160} />
<Table.Column title="访问次数" dataIndex="visited_num" width={120} />
<Table.Column title="转载数" dataIndex="reprinted_num" width={120} />
<Table.Column
title="操作"
cell={this.renderOperator}
lock="right"
width={120}
/>
</Table>

I think the issue may be that you're including an instance of <Router /> inside if the <Table.Column />. You shouldn't need to create a separate instance for every <Link />
Try removing the <Router /> inside of renderOperator

It took me a while to understand your question.
Firstly, please check if there is any console errors in your browser. If yes, it may block your UI router to view the page.
Secondly, if you are using react-router, here is a link to the hash router usage: https://reacttraining.com/react-router/web/api/HashRouter. Basically, you need to wrap the routes with <HashRouter>.
Lastly, a few tips to improve your code, not related to your question:
You can use <React.Fragment> to replace your <div> wrapper
You can use items.map() to iterate through the list of Table.Column.

Related

Would it be possible to disable the saved query without creating a new component in react-admin

In version 4 of react-admin, the functionality to save the filter was added. I would like to know how can I disable this function without creating another filter button
The Saved Queries in the sidebar, as shown in your screencast, are opt-in. It you don't want them, don't include the <SavedQueriesList /> component in your sidebar:
const PostFilterSidebar = () => (
<Card>
<CardContent>
- <SavedQueriesList />
<FilterList label="Category" icon={<CategoryIcon />}>
...
</FilterList>
<FilterList label="Published" icon={<DateRangeIcon />}>
...
</FilterList>
<FilterList label="Popularity" icon={<DateRangeIcon />}>
...
</FilterList>
</CardContent>
</Card>
);

how to change url without adding another level of path in react router dom 5

i have an api "xyz.com/products/" and i have made an route xyz.com/products/1. This will give me an product with the id 1 . And it will render product page Component with that product details...But now on that particular product page i have an gallery of related product with their own individual "Link" to open in same product page . but when i click on any one of them ....the url changes like this "xyz.com/product/1/23" but i want only to change path like "xyz.com/products/1" --> "xyz.com/products/23"
here is my code snippets
// ///// route and link both are on App Component/////////////////
// this is the route that i have that matches xyz.com/product/1
<Route
path="/product/:id"
render={() => (
<Fragment>
<ProductContext.Provider value={{ productState, productStateDispatch }}>
<Product />
<div className={`overlay ${productState.packageSidebar}`}></div>
</ProductContext.Provider>
</Fragment>
)}
/>;
these are links will go on product page
<Row title="Our best seller">
{homeState.bestSellerProducts.map((product, index) => (
<Link
to={{
pathname: `/product/${product.id}`,
search: `type=${product.type}`,
}}
className="product-link"
key={index}
>
{" "}
<Card {...product} />
</Link>
))}
</Row>
//////////////////////////////
// this is in my product component where i have used one more component called imageGallery inside it. in which we have links////////////
{images.map((imgObj, index) => {
console.log(imgObj);
return (
<div className="item" key={index}>
<Link to={`/${imgObj.id}`} className="product-link">
<div className="img">
<img
height={100}
width={100}
src={imgObj.product_image}
alt="product-img"
/>
</div>
</Link>
</div>
);
})}
// i know i messing in routing i hope will get solution.... i just want that when i click on any link in this imageGallery component .it should be open inside same product page in which i m already present.. thats it
Including exact keyword in the Route component might solve your problem, try out!, demo example:
<Route exact to='/' component={home} />

How to reset useState everytime I change my page?

I'm working on a Hamburger Navbar
here is my page look like:
the issues is whenever I clicked to another route but the navbar still appear on right side. I want to make it whenever I go to another route it will disappear.
Here is my code:
const App = (props) => {
const [ menuOpen, setMenuOpen ] = useState(false);
<HamburgerNav>
<HamburgerNavContainer>
<LogoLinkStyled to="/">
<LogoNav src="https://thebeuter.com/wp-content/uploads/2020/04/logo-black.png" />
</LogoLinkStyled>
<HamburgerUtilities>
<HamburgerUlityItem>
<Icon className="fal fa-search fa-rotate-90" onClick={openModalHandler} />
</HamburgerUlityItem>
<HamburgerUlityItem>
<Link to="/cart" style={{ color: 'black', textDecoration: 'none' }}>
<Icon className="fal fa-shopping-bag" />
<CartNumb>({props.basketProps.basketNumbers})</CartNumb>
</Link>
</HamburgerUlityItem>
<HamburgerUlityItem>
<HamburgerLine onClick={() => setMenuOpen(!menuOpen)} />
</HamburgerUlityItem>
</HamburgerUtilities>
</HamburgerNavContainer>
</HamburgerNav>
How can I fix this problem? Really appreciate it.!!!
Github project: https://github.com/nathannewyen/the-beuter
update Router code:
Here is my Router for all routes in navbar
<Router>
<ContactForm path="/contact" />
<SizeChart path="/size-chart" />
<ShippingAndReturn path="/shipping-return" />
<PrivacyAndPolicy path="/privacy-policy" />
<AboutUs path="/about-us" />
<ShopAllProducts path="/" />
<NewArrival path="/shop/new-arrival" />
<Tops path="/product-category/top" />
<Bottoms path="/product-category/bottom" />
<Product path="/product/:title_url" />
<SearchInfo path="/search/:title" searchTerm={searchTerm} title="Profile" />
<Cart path="/cart" />
<Checkout path="/checkout" />
</Router>
You need to add onClick to the "route" component that is not closing it and do something like this:
onClick={() => { setMenuOpen(prevState => {return !prevState}) }}
If for example you want it to close when you click on "T-Shirts", then "T-Shirts" must also have that onClick.
If you already have onClicks on these components with another function, you can just add multiple functions inside the anonymous function like this:
onClick={() => {
YourOtherFunction();
setMenuOpen(prevState => {return !prevState});
}
If your components are not in the App.js you need to somehow pass the onClick down too them.
Since its a state you wont be able to pass down the setMenuOpen itself, you need a wrapper function. So first create the wrapper:
const setMenuOpenWrapper = () => {
setMenuOpen(prevState => return { !prevState });
}
Then pass it down to the childrens like:
and then inside your ContactForm on the link to the contact form add the onClick:
...onClick={() => { closeMenuFunction(); }}
Ok I just checked your code, you need to pass down the function to the Sidenav component.
So in your App.js first create the wrapper function as I explained above
After that again in App.js on line 316 where you have <SideNav menuOpen={menuOpen} /> change it to <SideNav menuOpen={menuOpen} closeMenuFunction={setMenuOpenWrapper}/>
Then in your Sidenav.jsx on all of your menu items add an onclick:
onClick={props.closeMenuFunction}
And then it should work.

How to use <br/> in string to put string in new line?

I was creating React project and here is the component that I want to ensure that is put inside a string.
<InfoIcon
tooltip={`${hints.todoHint} <br/> ${hints.inProgressHint} <br/> ${hints.doneHint}`}
/>
but it is not working since br/ is literally rendered like br/
One option is to convert tooltip in to three props and add <br /> in the InfoIcon component. For example InfoIcon component can be
const InfoIcon = ({ todoHint, inProgressHint, doneHint }) => (
<div>
{todoHint}
<br />
{inProgressHint}
<br />
{doneHint}
</div>
);
// Using Info Icon
<InfoIcon todoHint={todoHint} inProgressHint={inProgressHint} doneHint={doneHint} />
Other option is to send tooltip as follows
const tooltip = (
<div>
{hints.todoHint}
<br />
{hints.inProgressHint}
<br />
{hints.doneHint}
</div>
)
<InfoIcon tooltip={tooltip} />
Well, material-ui tooltip allows you to use ANY kind of HTML content. => refer to official document customized tooltiops
This means you can use normal <div> and <Typography />, and any other styled elements to handle multi-line content.
The only thing you need to do is pass the content to props title => refer to document of tooltip api
import {
Tooltip,
Typography
} from '#material-ui/core';
<Tooltip
title={ // customized content here via props `title`
<>
<div>Seperate line</div>
<Typography>Seperate line</Typography>
</>
}
>
<IconButton aria-label="delete">
<InfoIcon /> // You can use your original icon here
</IconButton>
</Tooltip>
Watch it online: https://stackblitz.com/run
You can find related question here: How to make line break for ToolTip titles in Material-UI

Why href does not work on server in app react?

I am creating a project in react but I came across a problem when creating the responsive menu. That is, when using a menu that uses href it does not work on the apache server and Link To does not solve my problem. I have this code:
return (
<div>
<ResponsiveMenu
menuOpenButton={<FaBars size={30} color="MediumPurple" />}
menuCloseButton={<FaRegWindowClose size={30} color="MediumPurple" />}
changeMenuOn="500px"
largeMenuClassName="large-menu"
smallMenuClassName="small-menu"
menu={
<Menu>
<ul>
<li>INICIO</li>
<li>PROJETO</li>
<li>PARCERIA</li>
<li>PLANTAS & LÃ</li>
<li>RECURSOS</li>
<li>CONTACTOS</li>
</ul>
</Menu>
}
/>
</div>
);

Resources