Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/435.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何从react表中单击表行转到另一页?_Javascript_Reactjs_React Router_React Table - Fatal编程技术网

Javascript 如何从react表中单击表行转到另一页?

Javascript 如何从react表中单击表行转到另一页?,javascript,reactjs,react-router,react-table,Javascript,Reactjs,React Router,React Table,当我单击表行时,无法转到另一页 我已经在使用gettdrops函数从表行中获取属性,当我在特定行上单击时,它正在为此工作。 问题是,当我得到“onClick”事件时,我不知道如何使用“react router”转到另一个屏幕。 我也不想使用“react router”中的“link”函数,因为这个函数用蓝色突出显示表中的单词 ```export default class Lista2 extends Component{ constructor(){ s

当我单击表行时,无法转到另一页

我已经在使用gettdrops函数从表行中获取属性,当我在特定行上单击时,它正在为此工作。 问题是,当我得到“onClick”事件时,我不知道如何使用“react router”转到另一个屏幕。 我也不想使用“react router”中的“link”函数,因为这个函数用蓝色突出显示表中的单词

```export default class Lista2 extends Component{
    constructor(){        
        super();
        this.state = {
            list: []
        }                
    }

    componentDidMount() {
        this.getList();       
    }

    getList(){
        axios.put(proxyurl + url, {
            "owner": 2
        }).then(res => {      
            this.setState({ list: res.data})
       })
   }

   render(){ 
        const columnsTable = [
                {
                    Header: "ID",
                    accessor: "id",
                    width: 120,
                },
                {
                    Header: "Setor",
                    accessor: "setor"
                },
                {
                    Header: "Máquina",
                    accessor: "maquina"
                },
                {
                    Header: "Sensor",
                    accessor: "sensor"
                }]

        let tableStyle = {
            //backgroundColor: "#FCFD96",
            marginRight: "auto",
            marginLeft: "auto",
            marginBottom: "20px",
            fontSize: "14px",
            textAlign: "center",
        }

        return( 
            <Main {...headerProps}>
                <div>                              
                    <ReactTable
                        data= {this.state.list}
                        columns={columnsTable}
                        showPaginationBottom= {true}
                        className= "-striped -highlight"
                        style={tableStyle}
                        noDataText = {"Please Wait"}                        
                        getTdProps={(state, rowInfo, column, instance) => {
                            return {
                              onClick: (e, handleOriginal) => {
                                console.log('A Td Element was clicked!')
                                console.log('it produced this event:', e)
                                console.log('It was in this column:', column)
                                console.log('It was in this row:', rowInfo)
                                console.log('It was in this table instance:', instance)
                              }
                            }
                          }}

                    />    
                </div>              
            </Main>
        )        
    }
} ```
``导出默认类Lista2扩展组件{
构造函数(){
超级();
此.state={
名单:[]
}                
}
componentDidMount(){
这个.getList();
}
getList(){
axios.put(proxyurl+url{
“业主”:2
}).然后(res=>{
this.setState({list:res.data})
})
}
render(){
常数列稳定=[
{
标题:“ID”,
访问者:“id”,
宽度:120,
},
{
标题:“Setor”,
存取器:“设置器”
},
{
标题:“Máquina”,
访问者:“maquina”
},
{
标题:“传感器”,
存取器:“传感器”
}]
让表样式={
//背景颜色:“FCFD96”,
marginRight:“自动”,
marginLeft:“自动”,
marginBottom:“20px”,
字体大小:“14px”,
textAlign:“居中”,
}
报税表(
{
返回{
onClick:(e,handleOriginal)=>{
console.log('单击了Td元素!')
console.log('它产生了这个事件:',e)
console.log('它在此列:'中,第列)
console.log('它位于此行:',rowInfo)
log('它在这个表实例中:',实例中)
}
}
}}
/>    
)        
}
} ```
我想知道如何转到另一个屏幕,单击表中的整行? 我应该调用另一个函数吗?
我应该使用react路由器的哪些功能?

您可以使用
历史记录。从
react路由器推送

import { withRouter } from 'react-router'

class Lista2 extends Component {
  ....
  onClick: (e, handleOriginal) => {
    this.props.history.push('some-page')
    console.log('A Td Element was clicked!')
    console.log('it produced this event:', e)
    console.log('It was in this column:', column)
    console.log('It was in this row:', rowInfo)
    console.log('It was in this table instance:', instance)
  }
}

export default withRouter(Lista2)

在getTdProps功能中,您可以使用此功能重定向到应用程序的另一个路由:

window.location(“您的_路线”);
例如:

gettdrops={(状态、行信息、列、实例)=>{
返回{
onClick:(e,handleOriginal)=>{
窗口位置(“/#/home”)
}
}
}}

如果路由器中的路径
/home
指向一个新页面,则该路径将重定向到
www.reactApp.com/#/home

您能否提供一个代码笔示例?谢谢您的回答。我可以使用window.location将道具传递到另一个页面吗?我该怎么做?不,你实际上不能,因为它完全改变了你所在的页面,所以它将“丢失”你在当前页面上的道具/状态。但是,您可以在路由中添加一个变量作为快速修复。虽然这不是最漂亮的东西,但有助于将您需要的一些信息传递到新页面。我收到以下错误-->“Lista2.jsx:134未捕获的TypeError:window.location不是函数”我必须在之前声明“window.location”吗?不,您不需要,
window
通常是javascript固有的,所有浏览器都支持它,查看有关window.location的详细信息。奇怪的是,它返回了一个错误,但你肯定写得正确吗/我又试了一次,但没有成功。无论如何,我需要通过单击行将道具传递到另一个页面。我正在尝试使用gettdrops函数下面的“Link”命令,但它不起作用。你们知道我是否可以在代码的那个位置使用react路由器的“链接”吗?它成功了!我可以用withRouter传递道具吗?我该怎么做?否。
使用路由器
帮助您访问
历史
对象。您可以使用
历史记录进入另一个页面,按