简体   繁体   English

将道具传递给反应路由器路由的最佳方法是什么?

[英]What is the best way to pass in props to a react router route?

I have a react component I need to render that takes one argument of a string when it is initialized.我有一个需要渲染的反应组件,它在初始化时采用字符串的一个参数。 I need a button to click on that will redirect and render this new component with the string.我需要一个按钮来单击它将重定向并使用字符串呈现这个新组件。 It sends the string I want when it console.log(pro).它在 console.log(pro) 时发送我想要的字符串。 everytinme I click on the button it goes to a blank screen and doesn't load.每次我点击按钮,它都会进入一个空白屏幕并且不加载。

My routes.js looks like我的 routes.js 看起来像

const Routes = (props) => {
  return (
      <Switch>

        <Route exact path="/member" component={() => <Member user={props.state.member} />} />

        <Route path="/posts" exact component={Posts} />

        <Redirect exact to="/" />
      </Switch>
    
  )
}

export default Routes

The original component looks like this原始组件看起来像这样

const Posts = (props) => {
  const dispatch = useDispatch();
  const [postCount, setPostCount] = useState(0);
  const [member, setMember] = useState({});

  const getProfile = async (member) => {
    const addr = dispatch({ type: 'ADD_MEMBER', response: member })
    console.log(member)
  props.history.push('/member'
  );
  console.log('----------- member------------') //   console.log(addr)

   return (
    <Member user={member}><Member/>
  );
  }


  return (
      <div>
        {socialNetworkContract.posts.map((p, index) => {
          return <tr key={index}>

  
    <button onClick={() => getProfile(p.publisher)}>Profile</button>
        </tr>})}
      </div>
  )
}

export default withRouter(Posts);

The component I'm trying to render from the Posts component needs to be rendered with a string我试图从 Posts 组件呈现的组件需要用字符串呈现

const Member = (props)=> {  
  const [user, setUser] = useState({});
  const { state } = props.location;
  const [profile, setProfile] = useState({});


useEffect(() => {
  const doEffects = async () => {
    try {
      const pro = socialNetworkContract.members[0]
      console.log(pro)
      const p = await incidentsInstance.usersProfile(pro, { from: accounts[0] });
      setProfile(p)

    } catch (e) {
      console.error(e)
    }
  }
    doEffects();
  }, [profile]);


  return (
    <div class="container">
{profile.name}
    </div>
  )
}

export default Member;

You can pass an extra data to a route using state attribute with history.push您可以使用带有 history.push 的 state 属性将额外数据传递给路由

const getProfile = async (member) => {
    const addr = dispatch({ type: 'ADD_MEMBER', response: member })
    console.log(member)
    props.history.push({
      path: '/member',
      state: { member } 
    });
}

Once you do that you can access it in the rendered route from location.state完成后,您可以从 location.state 在渲染的路线中访问它

import {
  useLocation
} from "react-router-dom";
const Member = (props)=> {  
  const [user, setUser] = useState({});
  const { state } = useLocation();
  console.log(state.member);
  const [profile, setProfile] = useState({});


  ...
}

export default Member;

Also you do not need to pass on anything while rendering the Route此外,您在渲染路线时不需要传递任何东西

const Routes = (props) => {
  return (
      <Switch>

        <Route exact path="/member" component={Member} />

        <Route path="/posts" exact component={Posts} />

        <Redirect exact to="/" />
      </Switch>
    
  )
}

export default Routes

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

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